pymongo find() 与 mongodb find() 相比,pymongo find() 提供的有关文档的数据较少
我有一个合作伙伴集合
,我正在使用 pymongo
检索数据
当我使用MongoDB查询集合时,我看到以下结果
db.partner.find({'unique_key': 'c89dbe313932008febde61cdd2a071a1d'},{})
{ "_id" : ObjectId("4eb463cb158acb554e8c9c11"), "unique_key" : "c89dbe313932008febde61cdd2a071a1d", "name" : "ABC", "primary_key" : 12 }
但是当我通过pymongo查询时,这就是我所做的
for document in collection.find(find, criteria):
print document
where find = {'unique_key': 'c89dbe313932008febde61cdd2a071a1d'} and
criteria = {}
这是我在结果中看到的:
{u'_id': ObjectId('4eb463cb158acb554e8c9c11')}
并且我不知道没有得到 name 和 primary_key 结果,我错过了什么吗?
谢谢
I have a partner collection
and I am using pymongo
to retrieve the data
When I query the collection with MongoDB, I see the following result
db.partner.find({'unique_key': 'c89dbe313932008febde61cdd2a071a1d'},{})
{ "_id" : ObjectId("4eb463cb158acb554e8c9c11"), "unique_key" : "c89dbe313932008febde61cdd2a071a1d", "name" : "ABC", "primary_key" : 12 }
But when I query via pymongo, here is what I do
for document in collection.find(find, criteria):
print document
where find = {'unique_key': 'c89dbe313932008febde61cdd2a071a1d'} and
criteria = {}
Here is what I see in result:
{u'_id': ObjectId('4eb463cb158acb554e8c9c11')}
and I don't get name and primary_key in result, am I missing something?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
似乎当您传递空字典(您的
criteria
变量)作为第二个参数时,这意味着您不希望返回任何字段(除了总是返回的_id
)。find()
的第二个参数定义您想要的字段。尝试设置criteria=None
或根本不通过criteria
。链接到 pymongo 文档对于
find()
。It seems that when you pass empty dictionary (your
criteria
variable) as the second parameter it means you want no fields returned (except_id
that is always returned). The second parameter forfind()
defines the fields that you want. Try to setcriteria=None
or not passcriteria
at all.Link to the pymongo document for
find()
.这是由于 mongodb shell 将
{}
解释为字段选择器的方式与 pymongo 如何解释空字典的方式有关。本质上,shell 会忽略作为字段选择器的空对象,而 pymongo 将其解释为“不返回任何内容(除了默认的_id
)”。使用 pymongo 或 mongodb shell 时不需要指定字段选择器(可以将其留空)。因此,shell中的这条语句:
相当于:(
这条语句在shell和pymongo中都有效)
This is due to how the mongodb shell interprets
{}
as a field selector vs. how pymongo interprets an empty dictionary. In essence, the shell ignores the empty object as a field selector, whereas pymongo is interpreting it as "return nothing (except the default of_id
)".You don't need to specify a field selector when using pymongo or the mongodb shell (you can just leave it blank). Thus, this statement in the shell:
is equivalent to:
(this statement will work in both the shell and pymongo)