检查 Pymongo 中是否存在字段

Check Field Exist in Pymongo

我有一个 collection 这样的:

{ 
  "_id":"1321464"
  "Sex":"Male"
  "Age":"20" 
  "City":"Toronto" #Maybe this field are not present.
}

我想查找字段 "City" 不存在的所有文档。 我试试 :

collection.find({"sex":"Male"},{"City":{"$exists": False}},{'Age': 1, '_id':0})

我收到了这条错误消息:

File "/usr/local/lib/python2.7/dist-packages/pymongo/collection.py", line 1239, in find
    return Cursor(self, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/pymongo/cursor.py", line 125, in __init__
    raise TypeError("skip must be an instance of int")
TypeError: skip must be an instance of int

您将三个参数传递给 find 方法。我假设您打算仅通过过滤器和投影。试试这个:

collection.find({"sex":"Male", "City":{"$exists": False}},{'Age': 1, '_id':0})