【问题标题】:How to fetch all the documents of the MongoDB collection in Python如何在 Python 中获取 MongoDB 集合的所有文档
【发布时间】:2020-11-12 08:45:40
【问题描述】:

我想获取某个 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 查看包含数千个对象/文档的集合时,运行它后屏幕上没有打印任何内容。

我是 MongoDB 新手。

【问题讨论】:

    标签: python mongodb pymongo


    【解决方案1】:

    这将是一个疯狂的猜测,但我怀疑你指定了一个不正确的集合名称:

    db_collection_users = db.pg_new_sw
    

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

    【讨论】:

    • 谢谢 :) !傻我..这是错误,虽然我重新检查了三次,但仍然无法弄清楚
    • @POOJAGUPTA 唷,做出这个猜测是有风险的,但很高兴看到它已经解决了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-30
    • 2012-06-04
    • 2021-06-29
    • 1970-01-01
    • 2023-03-28
    • 2021-07-05
    • 2019-10-19
    相关资源
    最近更新 更多