1

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.

1
  • 1
    NUUID does not exist, what r u talking about? UUID(<string>) instead works fine. Also $eq:[{"$$a":"b"}] should be $eq:[{"$$a","b"}] with a comma between the things you compare Commented Jan 7, 2021 at 22:35

1 Answer 1

1

For me is working (sharded cluster version 4.4.3):

mongos> db.e.find()
{ "_id" : ObjectId("512e28984815cbfcb21646a7"), "myList" : [ { "a" : true, "b" : "mydata1" }, { "a" : true, "b" : "mydata2" }, { "a" : false, "b" : "mydata3" }, { "a" : false, "b" : "mydata4" }, { "a" : true, "b" : "mydata5" } ] }
{ "_id" : ObjectId("512e28984815cbfcb21646a8"), "myList" : [ { "a" : true, "b" : "mydata1" }, { "a" : true, "b" : "mydata2" }, { "a" : false, "b" : "mydata3" }, { "a" : false, "b" : "mydata4" }, { "a" : true, "b" : "mydata5" }, { "a" : true, "b" : "mydata6" } ] }

mongos> db.e.aggregate({$project: { myList: { $filter:{  input:"$myList" , as:"element" , cond: {$eq:[ "$$element.a",true  ] }               }   }         }})
{ "_id" : ObjectId("512e28984815cbfcb21646a7"), "myList" : [ { "a" : true, "b" : "mydata1" }, { "a" : true, "b" : "mydata2" }, { "a" : true, "b" : "mydata5" } ] }
{ "_id" : ObjectId("512e28984815cbfcb21646a8"), "myList" : [ { "a" : true, "b" : "mydata1" }, { "a" : true, "b" : "mydata2" }, { "a" : true, "b" : "mydata5" }, { "a" : true, "b" : "mydata6" } ] }
mongos> 
Sign up to request clarification or add additional context in comments.

1 Comment

I used robomongo for nuuid conversion, but that doesn't relate to the question, in any case the equaliy was the issue thanks for the example kiko075

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.