我认为这是一个相当基本的问题,但我无法解决这个问题。所以,基本上,在我的代码中,我已经隔离了一个文档,如下所示:
document = collection.find_one(name)
现在我有了这份文件,我想知道如何为这份特定文件打印出某个密钥。所以,基本上,这个文件现在看起来像这样:
{
"_id" : ObjectID("...")
"name": ABCD
"info": {
"description" : "XYZ"
"type" : "QPR"
}}
我想知道如何提取和打印" XYZ"使用变量" document"其中包含整个文档。
答案 0 :(得分:1)
document
是regular Python dictionary:
print(document["info"]["description"])
PyMongo Tutorial中涵盖了许多相关的PyMongo基本用法信息 - 请务必对其进行研究。
答案 1 :(得分:0)
这只是一本字典
print(document['info']['description'])
或者如果您不确定您的文档是否包含正确的密钥
info = document.get('info', None) if info: print(document.get('description', 'No description'))