3

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 Answer 1

8

Replacing false with False in method 4, or null with None in 2 works.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.