psykx
psykx

Reputation: 293

Not in document field array mongodb with $expr

I'm trying to get all documents that don't have an _id in the array excluded.

db.sites.find({ "$expr": { '_id': { "$not": { "$in": "$excluded"} } } });

I'm not using $nin because it's not allowed under $expr.

I'm getting the following error message:

Error: error: {
    "ok" : 0,
    "errmsg" : "Expression $in takes exactly 2 arguments. 1 were passed in.",
    "code" : 16020,
    "codeName" : "Location16020"
}

Can I use $where for this instead?

Upvotes: 2

Views: 1252

Answers (1)

mickl
mickl

Reputation: 49985

The $in operator requires two arguments:

db.sites.find({ $expr: { $not: { $in: [ "$_id", "$excluded" ] } } })

Working example here

Upvotes: 6

Related Questions