I'm looking for a way to query a collection like this:
[{
name: "Parent 1",
items: [
{ name: "Child 1", age: "60" },
{ name: "Child at heart", age: "27" }
]
},
{
name: "Parent 2",
items: [
{ name: "Child 3", age: "10" },
{ name: "Child 4", age: "15" },
{ name: "Child at heart", age: "60" }
]
}
]
With a query like this:
db.collection.find( 'items.name' => "Child at heart", 'items.age' => "60" )
And get a result of just the object with name "Parent 2". That is, I want to query the documents looking for the ones that contain an embedded item that has both a name of "Child at heart", and an age of "60" in the same embedded document. Instead this query returns both documents, "Parent 1" and "Parent 2".
How do I query with an $and condition on different fields within the same embedded document?
Thanks.