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
- Find restaurants whose
address.zipcodeis exactly10011. easy restaurants - Return each restaurant's
nameandaddress.street, without_id. easy restaurants - Find movies with more than 100000
imdb.votes. easy movies - Find movies that have won zero awards. Return
titleandawardsonly, no_id. medium movies - Find orders that include the product with sku
KB-100. medium orders - Find restaurants on a street containing
Ave. Returnname,address.streetandborough, no_id. medium restaurants - Find movies with an
imdb.ratingbelow 6.5. Returntitleandimdb.ratingonly, no_id. hard movies - Find employees who report to
E4. Returnnameandtitlewithout_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?
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?
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?
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