Query embedded fields and arrays with dot paths. Array queries can match any element or specific positions.
Embedded fields
db.users.find({ 'profile.locale': 'en-US' })
db.users.find({ 'profile.displayName': /^Ada/ })Practice: Run in mongosh on your practice database.
Array contains
db.products.find({ tags: 'sale' })
db.products.find({ tags: { $all: ['sale', 'new'] } })
Element match
db.orders.find({
lines: { $elemMatch: { sku: 'A1', qty: { $gte: 2 } } }
})$elemMatch requires one array element to satisfy all listed conditions.
Important interview questions and answers
- Q: tags: 'sale' meaning?
A: Matches documents where tags array contains that value. - Q: $elemMatch vs dot on array?
A: $elemMatch keeps conditions on the same array element.
Self-check
- Query users with locale en-US.
- When is $elemMatch required?
Pitfall: Forgetting $elemMatch when multiple conditions must apply to the same array element.
Interview prep
- $elemMatch?
- All conditions apply to same array element.
- tags: 'sale'?
- Matches if array contains value.