Understanding Documents, Fields, Arrays, and Embedded Documents

MongoDB's document model is one of its most important concepts. A document is similar to a structured record, but its values can include complex nested structures.

Consider this course document:

{
  _id: ObjectId("65a000000000000000000010"),
  slug: "mongodb-fundamentals",
  title: "MongoDB Fundamentals",
  published: true,
  price: 499,
  tags: ["mongodb", "database", "backend"],
  instructor: {
    name: "Kavitha",
    country: "India"
  },
  createdAt: ISODate("2026-01-10T10:00:00Z")
}

Scalar fields

Fields such as title, published, and price contain scalar values: a string, Boolean, and number respectively.

Arrays

The tags field is an array. Arrays can contain strings, numbers, documents, or other supported values.

For example:

{
  title: "Advanced Aggregation",
  prerequisites: [
    "mongodb-fundamentals",
    "mongodb-querying"
  ]
}

Arrays are useful when a document naturally owns a bounded list of related values. Unbounded arrays need special care because documents have a maximum BSON size and large arrays can make updates and queries expensive.

Embedded documents

The instructor field contains another document. Nested values can be queried using dot notation:

db.courses.find({ "instructor.country": "India" });

The dot in instructor.country identifies a nested field path. It is not a literal field name separator in the stored structure.

BSON types

MongoDB uses BSON, a binary-encoded serialization format based on a JSON-like document model. BSON supports additional types and distinctions that ordinary JSON does not represent directly, including ObjectId, Date, Decimal128, binary data, and several integer types.

For instance, an actual BSON Date is different from a string that merely looks like a date. That distinction matters when filtering by date ranges, sorting chronologically, and calculating date differences.

{
  createdAt: ISODate("2026-10-22T09:30:00Z"),
  expiresAt: null,
  version: NumberInt(3)
}

This example uses MongoDB shell notation to illustrate BSON values. ISODate() and NumberInt() are shell helpers, not JSON syntax.

Documents have a size limit

MongoDB BSON documents have a maximum size of 16 MiB. This limit affects document design, especially when embedding large arrays, storing binary data, or accumulating historical events in a single document.

The solution is not always to split every nested value into its own collection. Instead, model documents around access patterns, relationships, cardinality, and expected growth.

Insert and inspect a document with nested fields
db.courses.insertOne({
  slug: "mongodb-fundamentals",
  title: "MongoDB Fundamentals",
  published: true,
  tags: ["mongodb", "database"],
  instructor: {
    name: "Kavitha",
    country: "India"
  },
  createdAt: new Date("2026-10-22T09:30:00Z")
});

db.courses.find(
  { "instructor.country": "India" },
  { _id: 0, title: 1, "instructor.name": 1, tags: 1 }
);
💡 The first operation inserts one document into the courses collection. MongoDB generates an _id if one is not supplied. The second operation filters by a nested field and projects only the course title, instructor name, and tags. The date is created as a BSON Date using JavaScript's Date object. Run this in mongosh after connecting to your MongoDB deployment and selecting a database.
Key Takeaways
  • Documents can contain scalar fields, arrays, and nested documents.
  • Dot notation addresses nested fields in queries.
  • BSON supports types beyond standard JSON.
  • A BSON Date is not equivalent to a date-formatted string.
  • MongoDB documents have a 16 MiB maximum BSON size.
Knowledge Check

Which query filters documents whose nested instructor country is India?