如何让 pymongo 的 find() 返回一个列表? [英] How to make pymongo's find() return a list?

查看:47
本文介绍了如何让 pymongo 的 find() 返回一个列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Pymongo 用它返回一个游标,我可以遍历结果并将它们的文档附加到列表中.有没有办法直接在列表中获取结果文档?谢谢

Pymongo returns a cursor with it I am able to iterate over the results and append their documents to a list. Is there a way to get the result documents in a list directly? Thanks

推荐答案

以下代码会将整个结果集(Cursor)转换成list:

The following code will convert the entire result set (Cursor) into a list:

myresults = list(mydb.mycollection.find())

这适用于相对较小的结果集,因为您将所有内容都放入内存中.

This is fine for relatively small result sets, as you are pulling everything into memory.

这篇关于如何让 pymongo 的 find() 返回一个列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆