我正在 python 中創(chuàng)建一個不和諧的機器人。我正在使用 MongoDB Atlas (NoSQL)我有一個看起來像這樣的用戶文檔"user": 12345,"created_at": 2012-12-31 01:48:24我想獲取集合中的每個文檔,然后將它的created_at.我怎樣才能做到這一點?我試過了db.inv.find({}),但沒有用。我查看了 MongoDB 的文檔,但他們只提到了 JavaScript。如何獲取我的集合中的每個文檔?
2 回答

夢里花落0921
TA貢獻1772條經(jīng)驗 獲得超6個贊
確保您的數(shù)據(jù)庫是 mongodb 中的數(shù)據(jù)庫。如果您的數(shù)據(jù)庫是 mongodb 的client.db,那么您的代碼是正確的。
MONGODB_URI = "mongodb://user:password@host:port/"
client= pymongo.MongoClient(MONGODB_URI)
# database
db = client.db
# db.collection.find({}) will get the item list
result = db.inv.find({})

慕虎7371278
TA貢獻1802條經(jīng)驗 獲得超4個贊
db.inv.find()將為您提供所有文檔的光標對象,然后您需要對其返回的文檔進行迭代以獲取指定的字段。
確保您已連接到正確的集合
result=db.inv.find()
for entry in result:
print(entry["created_at"])
添加回答
舉報
0/150
提交
取消