Reputation: 287
I have a problem with finding "all" names = y.
{
"_id" : 1000013,
"array" : [
{
"name" : "x"
},
{
"name" : "y"
},
{
"name" : "y"
}
]
}
I tried this solution:
db.users.find( { 'array.name': 'y'})
but it returns all value from the complete array
und
db.users.find( {
'array.name': 'y'
},{
array: { $elemMatch: { name: 'y' } }
} )
and this solution returns only one value because of $elemMatch- it returns only the first value to macht. I need the next output:
{
"_id" : 1000013,
"array" : [
{
"name" : "y"
},
{
"name" : "y"
}
]
}
is it possible?
Thanks!
Upvotes: 1
Views: 60
Reputation: 4421
db.users.aggregate([{
$match:{"array.name": "y"}
},{
$unwind:"$array"
},{
$match:{"array.name": "y"}
},{
$group:{_id:"$_id", array:{$push:"$array"}}
}]);
Upvotes: 3