使用pymongo查询空字段 [英] Querying for null field using pymongo

查看:978
本文介绍了使用pymongo查询空字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用python查询mongo中的null字段,但是它很难处理单词null或false.它要么给我一个错误,指出它们在python中未定义,要么在mongo中搜索字符串null和false,但我都不想发生这种情况.

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)错误,因为在字段中搜索字符串null/false而不是在字段不存在时进行搜索.
  • 2& 4)错误,因为在python中未定义null/false.
  • 任何帮助将不胜感激,我可以运行查询2& 4在mongo中并返回正确的文档,但是我需要从python shell运行它.

    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.

    推荐答案

    在方法4中将false替换为False,在2种方法中将None替换为None.

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

    这篇关于使用pymongo查询空字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆