MongoDB使用PyMongo打印漂亮

Van*_*xel 9 python printing mongodb pymongo

我已经查找了MongoDB的漂亮内容,我理解如何从shell中完成它.我找不到的是如何使用PyMongo,所以当我在eclipse中运行时,输出将打印漂亮而不是全部在一行.这就是我现在所拥有的:

  cursor = collection.find({})
  for document in cursor: print(document)
Run Code Online (Sandbox Code Playgroud)

这会打印我的集合中的所有内容,但我的集合中的每个文档只打印在一行中.我怎样才能改变它以使其打印漂亮?

mas*_*nun 26

PyMongo将文档作为Python数据结构提取.所以你可以pprint像这样使用它:

from pprint import pprint

cursor = collection.find({})
for document in cursor: 
    pprint(document)
Run Code Online (Sandbox Code Playgroud)