Reputation: 139
I have the following mongodb entries:
{
'someKey': 'someValue',
'array' : [
{'name' : 'test1',
'value': 'value1'
},
{'name' : 'test2',
'value': 'value2'
}
]
}
What would be the shortest query for finding 'value' for name = 'test2'?
Upvotes: 0
Views: 37
Reputation: 3244
Since you're querying on array of elements and you want filtered elemnts just unwind and match:
db.CollectionName.aggregate( [ { $unwind : "$array" } ,{$match:{'array.name':'test1'}} ])
This will give you a result like this:
{
"_id" : ObjectId("59df5b30646afdfce4084152"),
"someKey" : "someValue",
"array" : {
"name" : "test1",
"value" : "value1"
}
}
Upvotes: 1