1

I have the following document:

"_id" : 19,
"name" : "Elizabeth Moore",
"achronym" : "EM19",
"calc" : {
    "20" : {
        "role" : 20,
        "score" : 15,
        "inRole" : false,
        "range" : {
            "int" : 80,
            "min" : 20
        }

and I need to retrieve all _ids having "calc.inRole" false.

I tried:

db.coll.find({'calc.$.inRole': false})
db.coll.find({'calc.inRole': false})

but none of these worked.

How can I achieve that?

1 Answer 1

0

Since calc has fields with unknown keys you need to run $objectToArray to transofrm it into array of keys and values. Then you can run $in on that array. If you want to have it as single pipeline step you can use $let operator to define temporary variable:

db.collection.aggregate([
    {
        $match: {
            $expr:{ 
                $let: {
                    vars: {
                        arr: { $objectToArray: "$calc" }
                    },
                    in: {
                        $in: [ false, "$$arr.v.inRole" ]
                    }
                }
            }
        }
    },
    {
        $project: {
            _id: 1
        }
    }
])

Mongo Playground

Sign up to request clarification or add additional context in comments.

Comments

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.