3

I am new with mongoDB and I need to used $exists with $expr in mongoDB, i tried following:

$lookup: {
    as: "collectionDet",
    from: "collection",
    let: { userId: "$userId" },
    pipeline: [{
        $match: {
        $expr: {
            $and: [
            { $eq: ["$_id", "$$userId"]},
            { $eq: ["$status", true], },
            {
                $or: [
                { $eq: ["$isDeleted", false ], },
                { $exists: ["$isDeleted", false ] },
                ],
            }
            ],
        },
        },
    }],
}

this is return error like:

"message": "Unrecognized expression '$exists'"

Is there any way to used $exists in $expr or any other alternative available?

Dhaval
  • 868
  • 12
  • 22
  • 1
    Can you tell how your collection looks like and what are you trying to achieve. How to use $exists with $expr is something you should lookup in the documentation(https://docs.mongodb.com/manual/reference/operator/query) – vizsatiz Nov 27 '19 at 12:21
  • 1
    maybe you can use `isDeleted != true` instead of `isDelete == false || isDeleted missing` – Thilo Nov 28 '19 at 13:29
  • 1
    You can also pull everything but the first condition under `$and` out of the `$expr` and use them directly as filters under `$match`. Only the first one depends on an evaluated expressions, the others match against constants. – Thilo Nov 28 '19 at 13:33

0 Answers0