PyMongo can't get $ and + $ or work

I am using python 3, mongo version 2.6.4 and pymongo version 2.7. I can't get the following request to work ... This request is what I put right in the mongo shell:

db.zoo.find({'animal': {'$and': [{'$ne': 'dog'}, {'$ne': 'cat'}]}}).limit(20).pretty()

      

(Limit and cute is only there while I'm testing)
This is the error I'm getting:

error: {
    "$err" : "Can't canonicalize query: BadValue unknown operator: $and",
    "code" : 17287
}

      

I am getting the same error when I use the $ or operator:

error: {
    "$err" : "Can't canonicalize query: BadValue unknown operator: $or",
    "code" : 17287
}

      

I found Jira that was raised, but I was not sure if it was related to the same issue https://jira.mongodb.org/browse/SERVER-12981

I've tried every combination I can think of, but I can't seem to solve this problem.

Thanks in advance.

EDIT : I tried $ nin but returned nothing. Even though this is a Mongo shell request, I cannot get it to work in PyMongo and I get the same error

+3


source to share


1 answer


In this case, you can use implicit and

. The following query should work

db.zoo.find({'animal': {$ne: 'dog', $ne: 'cat'}})

      



to do and explicit

you can write

db.zoo.find({$and: [{'animal': {$ne: 'dog'}},{'animal': {$ne: 'cat'}}]})

      

+4


source







All Articles