Suppose we want retrieve only the queried element in an object array then we need to perform mongo query like below.
Let me explain with this help of example.
{
_id: 4,
zipcode: "63109",
students: [
{ name: "ankit", school: 99, age: 11 },
{ name: "amit", school: 99, age: 12 },
]
}
if we want the above document filtered with only array item age =12:
$elemMatch
We will use $elemMatch operator for this.
The $elemMatch operator limits the contents of an field from the query return the first element matching the $elemMatch condition.
db.schools.find( {"students.age": "19"},
{students: {$elemMatch: {age: "12"}}});
0 Comment(s)