Arrays & Nested Documents
Matching Array Elements
Matching any element versus matching the same element — the distinction that makes $elemMatch necessary.
Orders embed items. Each element is a document. A dotted path like items.unitPrice means some element has that field. Two dotted paths mean some element matches the first and some element matches the second — not necessarily the same one.
{
userId: ObjectId("64a1b2c3d4e5f6a7b8c90001"),
amount: 4900,
status: "completed",
items: [
{ name: "Mechanical Keyboard", quantity: 1, unitPrice: 4500 },
{ name: "USB-C Cable", quantity: 2, unitPrice: 200 }
]
}db.orders.find({
"items.unitPrice": { $gt: 500 },
"items.quantity": 2
}){ amount: 4900, items: [ { name: "Mechanical Keyboard", quantity: 1, unitPrice: 4500 }, { name: "USB-C Cable", quantity: 2, unitPrice: 200 } ] }There is no line with unitPrice > 500 and quantity: 2. The filter still hits. That is normal array matching: each condition is independent across the array. If that is what you wanted (any expensive item, and separately any qty-2 item), you are done. If you wanted one line item that is both, you need $elemMatch — next lesson.
A single dotted condition is fine: { "items.name": "USB-C Cable" } — at least one item has that name. The bug appears when you AND two conditions on the array of objects.
Interview question
Why can two conditions on an array of objects match different elements?
Each dotted path is evaluated against the array as a whole. items.unitPrice > 500 can be true of one line and items.quantity: 2 of another. The document matches even if no single line satisfies both.