在pyMongo中查找文档的所有字段

Ama*_*mar 5 mongodb pymongo

我想查找文档的所有字段,pymongo用于从中检索数据。下面的代码给出了所有文档及其所有字段。但是我们可以找到文档的所有字段或键吗?

f = db['Collection'].find()
for i in f:
    print(i['Date'])
Run Code Online (Sandbox Code Playgroud)

我可以ObjectId

print(db['Collection'].distinct("_id"))
Run Code Online (Sandbox Code Playgroud)

现在我们拥有ids所有文件。我们能找到字段吗?

Wan*_*iar 5

PyMongofind()方法返回一个Cursor实例,它允许您迭代所有匹配的文档。迭代中的文档是Python Dictionary,因此您可以使用方法列出其所有键keys(),例如:

cursor = db['collection'].find({})
for document in cursor: 
    print(document.keys())  # print all fields of this document. 
Run Code Online (Sandbox Code Playgroud)

综上所述,通常您应该了解集合中的字段。如果您想查询包含某些字段的文档,请参阅运算符$exists