Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct use of $ne or $not in pymongo (unsupported projection option)

I would like to write the following query in Mongo:

Get all rows where field equals var1 but/and not var2

I have this:

db["mydb"].find(
    {"field": var1},
    {"field": {
        "$ne": var2}
    }
)

But it yields the error that $ne is an "unsupported projection option."

like image 699
lennyklb Avatar asked Sep 08 '13 20:09

lennyklb


2 Answers

You can use the $and operator to combine requirements like this:

db["mydb"].find(
    {"$and": [
        {"field": var1},
        {"field": {
            "$ne": var2
        }}
    ]}
)
like image 140
JohnnyHK Avatar answered Oct 16 '22 00:10

JohnnyHK


Apart from the usage of $and, you can also fix it by using {} to combine the filters.

db["mydb"].find({
    "field": var1,
    "field": {"$ne": var2}
})
like image 31
Safwan Avatar answered Oct 16 '22 00:10

Safwan