pymongo find() 与 mongodb find(),pymongo find() 提供的关于文档的数据较少

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8128262/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 12:18:58  来源:igfitidea点击:

pymongo find() vs mongodb find(), pymongo find() gives less data about document

mongodbpymongo

提问by daydreamer

I have a partner collectionand I am using pymongoto retrieve the data
When I query the collection with MongoDB, I see the following result

我有一个partner collection,我正在使用它pymongo来检索数据
当我使用MongoDB查询集合时,我看到以下结果

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

但是当我通过pymongo查询时,这就是我所做的

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 nameand primary_keyin result, am I missing something?

我没有得到nameprimary_key结果,我错过了什么吗?

Thank you

谢谢

回答by Lycha

It seems that when you pass empty dictionary (your criteriavariable) as the second parameter it means you want no fields returned (except _idthat is always returned). The second parameter for find()defines the fields that you want. Try to set criteria=Noneor not pass criteriaat all.

似乎当您将空字典(您的criteria变量)作为第二个参数传递时,这意味着您不希望返回任何字段(除非_id始终返回)。的第二个参数find()定义了您想要的字段。尝试设置criteria=None或根本不通过criteria

Linkto the pymongo document for find().

链接到 pymongo 文档find()

回答by dcrosta

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)".

这是由于 mongodb shell 如何解释{}为字段选择器与 pymongo 如何解释空字典。本质上,shell 忽略空对象作为字段选择器,而 pymongo 将其解释为“不返回任何内容(除了默认值_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:

使用 pymongo 或 mongodb shell 时不需要指定字段选择器(您可以将其留空)。因此,shell 中的这个语句:

db.partner.find({'unique_key': 'c89dbe313932008febde61cdd2a071a1d'},{})

is equivalent to:

相当于:

db.partner.find({'unique_key': 'c89dbe313932008febde61cdd2a071a1d'})

(this statement will work in both the shell and pymongo)

(这个语句在 shell 和 pymongo 中都有效)