πŸƒDBQuestpractice MongoDB queries
0 / 110

MongoDB embedded documents and dot notation

Dot notation into nested objects and arrays of objects.  Β·  8 practice questions  Β·  Cheat sheet ↓

Documents nest, and dot notation is how you reach inside: { "address.zipcode": "10011" } filters on a nested field, { "address.street": 1 } projects it, and "imdb.votes" with $gt compares a nested number. The quotes are mandatory because of the dot. The same syntax walks into arrays of objects, which is why Array Queries and this topic share so many examples.

The trap is matching a whole embedded document. { address: { zipcode: "10011" } } is not a partial match: it matches only documents whose address is exactly that one-key object, in that order. Almost always you want the dot-notation field match instead. When you need several conditions on one nested object, dot notation serves a plain object and $elemMatch serves an array of objects.

MongoDB nested field practice questions

  1. Find restaurants whose address.zipcode is exactly 10011. easy restaurants
  2. Return each restaurant's name and address.street, without _id. easy restaurants
  3. Find movies with more than 100000 imdb.votes. easy movies
  4. Find movies that have won zero awards. Return title and awards only, no _id. medium movies
  5. Find orders that include the product with sku KB-100. medium orders
  6. Find restaurants on a street containing Ave. Return name, address.street and borough, no _id. medium restaurants
  7. Find movies with an imdb.rating below 6.5. Return title and imdb.rating only, no _id. hard movies
  8. Find employees who report to E4. Return name and title without _id. hard employees

Each question is answered in a live mongo-shell editor on this page; the result is checked against a reference solution.

Dot notation cheat sheet for nested fields

Dot notation, always quoted

db.restaurants.find({ "address.zipcode": "10011" })
db.movies.find({ "imdb.rating": { $gte: 8 } })
db.orders.find({ "items.sku": "KB-100" })

That last one reaches into an array of subdocuments – it matches if any

item has that sku.

Whole-document match is exact and ordered

{ address: { building: "31", street: "W 8th St" } }

This matches only documents whose address has exactly those two fields,

in that order. Almost never what you want – use dot notation.

Projecting nested fields

db.restaurants.find({}, { name: 1, "address.street": 1, _id: 0 })
// => { name: "...", address: { street: "..." } }

The shape is preserved; you get a trimmed address object, not a flat key.

Gotchas

  • { "a.b": 1 } in a filter = "field a.b equals 1".

{ "a.b": 1 } in a projection = "include a.b". Same syntax, different job.

Common questions

How do I query a nested field in MongoDB?

Use dot notation in quotes: db.restaurants.find({ "address.zipcode": "10011" }). It works at any depth ("a.b.c"), in projections ({ "address.street": 1 }) and in sorts. Unquoted, the dot is a JavaScript syntax error.

Why doesn't { address: { zipcode: "10011" } } match anything?

That is an exact match on the whole address subdocument: it must contain only zipcode, with that value, in that order. Real addresses have more fields, so nothing matches. Query the nested field with dot notation instead.

How do I project only some nested fields?

List them with dot notation: db.restaurants.find({}, { name: 1, "address.street": 1, _id: 0 }) returns name and an address object containing only street. You can also exclude nested fields with 0, but not mix include and exclude.

Continue:← Array QueriesAggregation Basics β†’

MongoDB topics:Finding & ProjectionComparison OperatorsLogical OperatorsElement & TypePattern MatchingSort, Limit & SkipArray QueriesAggregation BasicsGrouping & Accumulators$unwind & Array PipelinesJoins & Advanced StagesFull cheat sheet