Assuming I have this documents in mongodb:
{
"_id" : ObjectId("512e28984815cbfcb21646a7"),
"myList" : [
{
"a" : true,
"b" : mydata1
},
{
"a" : true
"b" : mydata2
},
{
"a" : false
"b" : mydata3
},
{
"a" : false
"b" : mydata4
},
{
"a" : true
"b" : mydata5
}
]
}
I want to get all array cells that have "a:true" in them.
I tried mongodb find(), but it only return single first match, which is a problem.
I ended up using $filter because I do not want to use $unwind (performance issues).
trying to execute this line, doesn't work:
.aggregate([
{$match: {_id: NUUID("d75dcc8d-ecb2-4d23-99d5-347bc99cfeec")}},
{$project:
{
myList:
{
$filter: {
input: '$myList',
as: 'myElement',
cond: {$eq: [{'$$myElement.a': true}]}
}}
}
}
])
But this doesn't seems to work, it says that $$ is not recognized. I wander what am I missing and also, why can't find() be used? it sound so basic to ask for some elements in array using a project.
UUID(<string>)instead works fine. Also$eq:[{"$$a":"b"}]should be$eq:[{"$$a","b"}]with a comma between the things you compare