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