如何获取 Python 中 MongoDB 集合的所有文档

How to fetch all the documents of the MongoDB collection in Python

我想获取 MongoDB 的某个集合的所有文档并打印每个文档中的某些属性。但是我做不到。

我的代码:

import csv

from pymongo import MongoClient

db_client = MongoClient()

db = db_client.pg_new_sw_cur

db_collection_users = db.pg_new_sw


#with open("pg_details_new.csv",'w') as f:

for post in db_collection_users.find():

        print post['user_name']
        print "==========================="

当使用 RoboMongo 查看时 运行 集合包含数千个 objects/documents 时,它在屏幕上不打印任何内容。

我是 MongoDB 的新手。

这是一个大胆的猜测,但我怀疑您指定的集合名称不正确:

db_collection_users = db.pg_new_sw

重新检查 pg_new_sw 是否确实存在于您的本地 MongoDB 服务器实例中并且其中包含文档。