Embedded Documents and Nested BSON Structures

An embedded document is a document stored as the value of a field inside another document.

Example:

{
 name: "Meera",
 address: {
 street: "Example Street",
 city: "Chennai",
 country: "India"
 }
}

The address field contains another document with its own fields.

Querying nested fields

Use dot notation to match a nested field:

db.users.find({ "address.city": "Chennai" });

Nested field projection

db.users.find(
 {},
 { _id: 0, name: 1, "address.city": 1 }
);

This projection returns the name and the nested city field while excluding _id.

Embedded document equality

MongoDB can compare an embedded document as a value, but exact embedded-document equality is stricter than checking a nested field. Field values and field order can matter in embedded-document equality comparisons.

For ordinary application queries, dot notation is often preferable when you want to match a particular nested property.

Embedded documents versus references

Embedding is useful when related information is commonly read and updated together and has an appropriate bounded size.

References are often useful when related records:

  • Have independent lifecycles.
  • Are shared by many parent records.
  • Grow without a practical bound.
  • Need to be queried independently.

For example, an address embedded in a user profile may be appropriate. A large history of transactions is generally better modeled separately.

Deep nesting

MongoDB supports nested documents, but extremely deep or complicated structures can make queries and application code difficult to maintain. The design should represent the domain clearly rather than maximizing nesting depth.

Field names containing dots or dollar signs

Dot notation uses dots to address nested paths, so ordinary application field names should avoid ambiguity with path syntax. MongoDB has restrictions and special handling for field names containing periods or beginning with dollar signs, with limitations across operations and tools. Prefer conventional field names unless a specific requirement justifies an unusual name.

Query nested documents
db.embeddedExamples.insertOne({
 name: "Meera",
 address: {
 city: "Chennai",
 country: "India"
 },
 preferences: {
 theme: "dark",
 notifications: {
 email: true,
 sms: false
 }
 }
});

// Query a nested city.
db.embeddedExamples.find({ "address.city": "Chennai" });

// Query a deeply nested Boolean.
db.embeddedExamples.find({
 "preferences.notifications.email": true
});

// Project selected nested fields.
db.embeddedExamples.find(
 {},
 { _id: 0, name: 1, "address.city": 1 }
);
💡 The example demonstrates nested objects, dot-notation filters, and nested projection. Dot notation lets the query target a particular property without requiring exact equality against the entire embedded document.
Key Takeaways
  • Embedded documents represent nested structures within a parent document.
  • Dot notation queries nested fields.
  • Exact embedded-document equality is stricter than matching a nested property.
  • Embedding and referencing should be selected according to access patterns and growth.
  • Unnecessarily deep nesting can make data models harder to maintain.
Knowledge Check

Which query selects users whose nested address city is Chennai?