MongoDB 使用 PyMongo 打印漂亮

MongoDB Print Pretty with PyMongo

我已经查找了 MongoDB 的漂亮印刷品,并且我从 shell 了解了如何做到这一点。我找不到的是如何使用 PyMongo 来完成它,这样当我在 eclipse 中 运行 它时,输出将打印得很好而不是全部打印在一行中。这是我现在拥有的:

  cursor = collection.find({})
  for document in cursor: print(document)

这会打印我的 collection 中的所有内容,但我的 collection 中的每个文档只打印一行。我怎样才能改变它让它打印得漂亮?

PyMongo 将文档作为 Python 数据结构获取。所以你可以像这样使用 pprint

from pprint import pprint

cursor = collection.find({})
for document in cursor: 
    pprint(document)