问题
I would like to query for a null field in mongo using python, however it struggles to deal with the words null or false. it either gives me the error that they are undefined in python or it searches for the string null and false in mongo, neither of which I want to happen.
collection = pymongo.MongoClient('mongodb://localhost:27017/')['historical'].highjump
1) data = pd.DataFrame(list(collection.find({"min":"null"})))
2) data = pd.DataFrame(list(collection.find({"min":null})))
3) data = pd.DataFrame(list(collection.find({"min":{"$exists":"false"}})))
4) data = pd.DataFrame(list(collection.find({"min":{"$exists":false}})))
- 1&3) error because searching for the string null/false in the field instead of searching for when the field doesn't exist.
- 2&4) error because null/false is not defined in python.
Any help would be greatly appreciated, I can run queries 2 & 4 within mongo and return the correct documents, but I need to run it from the python shell.
回答1:
Replacing false with False in method 4, or null with None in 2 works.
来源:https://stackoverflow.com/questions/43515590/querying-for-null-field-using-pymongo