liveBook · Manning

Dotted notation

As explained in MongoDB 8.0 in Action, Third Edition

Dot notation is MongoDB query syntax that accesses fields in nested documents or fields across array elements by joining path components with periods.

Dotted notation accesses fields inside nested documents and values across array elements by joining path components with periods. Quote the complete field path in query filters. section 4.9 section 4.9.1

Use the path form that matches the structure being queried:

  • Query a nested field directly, such as "airline.name" and "airline.id". section 4.9.1
  • Search any array element with a path such as "prices.price"; MongoDB evaluates that field across the array. section 4.9.3
  • Target a specific array position with a numeric component such as "prices.0.price"; array indexes start at zero. section 4.8 section 4.9.3
  • Use $elemMatch when multiple conditions must apply to the same embedded array element. section 4.9.3

Filter nested route data

db.routes.find({ "airline.name": "American Airlines", "airline.id": 413 }) This selects routes whose embedded airline has both specified values. For prices below 3000, use { "prices.price": { $lt: 3000 } }. section 4.9.1

Avoid brittle whole-document matches

Matching an entire embedded document requires every field and the expected field order. Prefer dotted paths when omitted, changed, or reordered fields should not invalidate the query. section 4.9.2

Listing 4.18 Using dot notation
db.routes.find({"airline.name": "American Airlines", "airline.id": 413})

Inspect a complete dotted query against nested airline fields. section 4.9.1

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Explains nested paths, array positions, quoting, and $elemMatch, while stopping short of broader query optimization.
Examples
Uses airline fields, route prices, customer accounts, tier details, and indexed array elements as query examples.
What it assumes
Assumes familiarity with MongoDB queries, embedded documents, arrays, and comparison operators.
What you can do after
Apply it via MongoDB query filters for nested fields, arbitrary array elements, indexed elements, and same-element conditions.
How it connects
Connects embedded documents, arrays, array indexes, exact matching, and the $elemMatch operator.

Also explained in

Compare how 9 books explain dotted notation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of MongoDB 8.0 in Action, Third Edition. The section links point to the parts of the book it draws on.