【问题标题】:Need to return cursor and not command_cursor with aggregate()需要返回 cursor 而不是 command_cursor 与 aggregate()
【发布时间】:2017-12-10 15:35:59
【问题描述】:

我需要迭代一个 aggregate() 结果两次。但是,无法回退由 aggregate() 返回的 command_cursor。我试图将聚合()的选项 UseCursor 设置为 True,但它总是返回一个 command_cursor。有没有办法做我想做的事?

主要问题是我无法将 aggregate() 返回的每个文档都存储在列表中...它使我的应用程序崩溃并在 ~127000 个文档后冻结我的虚拟机

【问题讨论】:

  • 尝试在第一次迭代中实现你想要做的任何事情,从 mongodb 中两次带来那么多文档是不好的。

标签: mongodb cursor aggregation-framework pymongo


【解决方案1】:

简单执行aggregate两次:

pipeline = [...]
for doc in collection.aggregate(pipeline):
    # do step 1 with your documents

for doc in collection.aggregate(pipeline):
    # do step 2 with your documents

事实上,这就是普通 Cursor 上的 rewind 实际所做的:它只是执行相同的查询两次。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-18
    • 2022-10-16
    • 1970-01-01
    • 2015-04-19
    相关资源
    最近更新 更多