mongodb array of objects code example
Example 1: search inside array with object mongodb
db.users.find({awards: {$elemMatch: {award:'National Medal', year:1975}}})
Example 2: find object in array mongodb
db.users.findOne({"_id": id},{awards: {$elemMatch: {award:'Turing Award', year:1977}}})
Example 3: how to get array object value in mongodb
db.products.aggregate([
{$unwind: "$ratings"},
{$match: {'ratings.user': ObjectId("5f6e2e0b70eb0208fc8401a1")}}
])
Example 4: how to get array object value in mongodb
db.test.aggregate([
{$match: {'shapes.color': 'red'}},
{$project: {
shapes: {$filter: {
input: '$shapes',
as: 'shape',
cond: {$eq: ['$$shape.color', 'red']}
}},
_id: 0
}}
])