如何将pymongo.cursor.Cursor转换为dict?

如何将pymongo.cursor.Cursor转换为dict?,第1张

如何将pymongo.cursor.Cursor转换为dict?

find
方法返回一个
Cursor
实例,该实例使您可以迭代所有匹配文档

要获得与给定条件匹配的第一个文档,您需要使用

find_one
。的结果
find_one
是字典。

您始终可以使用

list
构造函数来返回集合中所有文档的列表,但请记住,这将加载内存中的所有数据,并且可能不是您想要的。

如果需要重用游标并且有充分的理由不使用游标,则应该这样做

rewind()


演示使用

find

>>> import pymongo>>> conn = pymongo.MongoClient()>>> db = conn.test #test is my database>>> col = db.spam #Here spam is my collection>>> cur = col.find()  >>> cur<pymongo.cursor.Cursor object at 0xb6d447ec>>>> for doc in cur:...     print(doc)  # or do something with the document... {'a': 1, '_id': ObjectId('54ff30faadd8f30feb90268f'), 'b': 2}{'a': 1, 'c': 3, '_id': ObjectId('54ff32a2add8f30feb902690'), 'b': 2}

演示使用

find_one

>>> col.find_one(){'a': 1, '_id': ObjectId('54ff30faadd8f30feb90268f'), 'b': 2}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5644802.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-16
下一篇2022-12-16

发表评论

登录后才能评论

评论列表(0条)

    保存