Array Query
$elemMatch(query) - matches documents in a collection that contain an array field with at least one embedded document or value that matches all the specified criteria.
Data
{
records: [ { student: "Jane", grade: 90, location : "a" },
{ student: "Jane", grade: 78, location : "b" } ]
},
{
records: [ { student: "Jane", grade: 90, location : "c" },
{ student: "Bob", grade: 78, location : "d" } ]
}
Query
returns all documents in the collection where the records array contains at least one element with both student = Jane and grade > 85.
{ records: { $elemMatch: { student: "Jane", grade: { $gt: 85 } } } }
Result
matches { student: "Jane", grade: 90, location : "a" } and { student: "Jane", grade: 90, location : "c" }
Both documents are returned.
$elemMatch(query) - matches documents in a collection that contain an array field with at least one embedded document or value that matches all the specified criteria.
Data
{
records: [ { student: "Jane", grade: 90, location : "a" },
{ student: "Jane", grade: 78, location : "b" } ]
},
{
records: [ { student: "Jane", grade: 90, location : "c" },
{ student: "Bob", grade: 78, location : "d" } ]
}
Query
returns all documents in the collection where the records array contains at least one element with both student = Jane and grade > 85.
{ records: { $elemMatch: { student: "Jane", grade: { $gt: 85 } } } }
Result
matches { student: "Jane", grade: 90, location : "a" } and { student: "Jane", grade: 90, location : "c" }
Both documents are returned.
No comments:
Post a Comment