Document Basics

View saved

JSON-like, BSON under the hood

You write documents that look like JSON. MongoDB stores BSON, which adds types such as ObjectId, Date, and Binary.

Every document needs _id

If you omit _id, MongoDB assigns an ObjectId. It is unique within the collection and useful as a primary key.

db.users.insertOne({ name: "Lin", email: "[email protected]" })
db.users.findOne({ name: "Lin" })

Nest objects and arrays

Embed related data when you usually load it together. Keep arrays bounded so documents stay manageable.

{
  "title": "Todo app",
  "owner": { "name": "Ada" },
  "items": [
    { "text": "Ship MVP", "done": false }
  ]
}

Field names matter

Use consistent names and casing across the collection. Prefer clear names over abbreviations so queries stay readable.

Comments

One comment per signed-in account. Comments are saved with this page’s URL.