gpt4 book ai didi

python - 从 pymongo 检索数据

转载 作者:可可西里 更新时间:2023-11-01 09:08:21 24 4
gpt4 key购买 nike

我有一个 mongo 数据库,我想用 mongodb java api 中的游标操作检索所有文档。我想根据此游标迭代检索数据库的所有用户名。我的代码像上面这样:

import pymongo
from pymongo import MongoClient

client = MongoClient('...', 27017)
db = client.test_database
db = client['...']
collection = db.test_collection
collection = db["..."]
result = collection.find()
obj = next(result, None)
if obj:
username= obj['username']
print username

我想让集合打印所有用户名。

最佳答案

只需遍历结果并打印用户名。没有理由玩 next().

for obj in collection.find():
print obj['username']

关于python - 从 pymongo 检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23515467/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com