在 MongoDB 中重用 Cursor 对象

Reuse Cursor object in MongoDB

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

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

执行此操作的一种方法是使用 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 class 到return 文件列表。但这会将所有数据加载到内存中。