我是Python和MongoDB的初学者。我的数据库就像这样
{ '_id' : 1,
'identity' : {
'first_name' : 'John',
'last_name' : 'Doe',
'phone' : '+440823XXX'
}
'status' : 'active'
}
我想返回数据,哪个电话为空或不存在DB中。我尝试了
db.collection.find({"identity" : { "phone" : { "$exists": "true", "$ne": "null" } }})
,但它返回了<pymongo.cursor.Cursor at 0x7f5da40e8e80>
sintax如何获取查询哪个值不是空的?
请尝试此代码:
db.collection.find({"identity.phone" { $exists: "true", $ne: "null" } })
它将返回光标,所以
cur = db.collection.find({"identity.phone" { $exists: "true", $ne: "null" } })
您在cur
中获得了数据。只需循环。
for doc in cur:
...
以获取进一步的参考检查