Pymongo.find() 只返回答案

Max*_*xim 5 python mongodb pymongo python-2.7

我正在编写一个代码,该代码将使用 pymongo 从数据库中获取数据。之后,我将使用 Tkinter 在 GUI 中显示它。

我在用

.find()
Run Code Online (Sandbox Code Playgroud)

查找特定文件。但是,我不希望出现其他任何内容,然后显示“名称”。所以我使用了 {"name":1},现在它返回:

{u'name':u'**returned_name**'}
Run Code Online (Sandbox Code Playgroud)

如何删除u'name“:所以它只会返回returned_name

提前致谢,

最大限度

Ps 我在网上搜索了很多,但找不到任何可以帮助我的论据。

ale*_*cxe 6

你看到的find()调用返回的是一个cursor。只需遍历游标并通过name键为找到的每个文档获取值:

result = db.col.find({"some": "condition"}, {"name": 1})
print([document["name"] for document in result])
Run Code Online (Sandbox Code Playgroud)

结果,您将获得一个名称列表

或者,如果您希望并期望匹配单个文档,请使用find_one()

document = db.col.find_one({"some": "condition"}, {"name": 1})
print(document["name"])
Run Code Online (Sandbox Code Playgroud)