MongoDB Document Model

Context: FIT2094_MOC · a document store · JSON-style documents in schema-less collections · contrasts the normalised relational schema Parent Framework: NoSQL Data Models

Quick Revision

  • 🎯 Objective: store related data together as one flexible JSON/BSON document ➔ no JOIN to read a whole entity.
  • 📦 Core Components: database → collection → document → field | _id/ObjectId | embedded vs referenced.
  • ⚡ Key Constraint: the embed-vs-reference choice — embed when read together and stable; reference when large, shared, or independently updated.

📝 Core

1. Hierarchy & Terminology

MongoDBRelationalKey difference
Databasedatabase/schemasame
Collectiontableno fixed schema
Documentrownested objects + arrays; fields vary
Fieldcolumnoptional; may hold complex values

2. BSON & Identity

  • BSON ➔ binary JSON with extra types (dates, binary, ObjectId); you read/write JSON, Mongo stores BSON.
  • _id ➔ auto-added globally unique ObjectId (like a PK) if not supplied; a given _id you provide is used as-is.
  • Quote fields ➔ always double-quote field names to avoid parser ambiguity.
  • Unit convention ➔ store dates/times as strings (avoids timezone conversion, stays readable).

3. Embedded vs Reference

  • Embedded (denormalised) ➔ nest sub-documents inside the parent ➔ one read, no join.
  • Reference (normalised) ➔ store another document’s ObjectId ➔ app-layer “join” (not engine-enforced).

⚙️ Core Implementation

🔹 Embedded document (drone with rental array)

🔹 Generate the JSON from Oracle

⚖️ Core Decision Matrix

ApproachStoreBest whenCost
Embedded (denormalised)nested sub-docsalways read together; rarely changes aloneduplication across parents
Reference (normalised)ObjectId pointerlarge / shared / independently updatedapp must do the “join”

When It Flips: references mimic a foreign key but are not enforced by the engine — integrity is the application's responsibility, unlike Oracle's referential integrity.

🧠 Active Recall