Skip to main content
 首页 » 编程设计

python之在 MongoDB 中重用 Cursor 对象

2024年10月17日7haluo1

由于某些原因我需要使用同一个游标两次,一次用于迭代,一次用于函数,迭代后游标无法使用,所以我需要输入两次mongo命令。

我想让游标使用它两次,我尝试使用 lambda 作为游标,但系统告诉我“函数”对象不可迭代。

请您参考如下方法:

一种方法是使用 rewind()将游标倒回其未评估状态的方法。但正如文档中所述:

Future iterating performed on this cursor will cause new queries to be sent to the server, even if the resultant data has already been retrieved by this cursor.

如果您不想向服务器发送新查询,那么您可以使用list 类返回文档列表。但这会将所有数据加载到内存中。