Querying for null field using pymongo

て烟熏妆下的殇ゞ 提交于 2019-12-11 09:05:19

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!