Skip to main content

Can you filter by nested fields?

Yes, you can. In MongoDB, nested fields are filtered using dot notation.

Example:

js
db.users.find({ "address.city": "Berlin" })

If a document has a nested object:

json
{ "name": "Alex", "address": { "city": "Berlin", "street": "Main St." } }
  • then filtering by "address.city" finds it with no trouble.

So: nested fields are filtered the same way as ordinary ones, just using field.subfield.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.