如何在不超过最大文档大小的情况下编写聚合?

new*_*ike 8 mongodb pymongo

exceeds maximum document size problem通过查询例外如下,

pipe = [
    {"$match": { "birthday":{"$gte":datetime.datetime(1987, 1, 1, 0, 0)} }}
    ]
res =db.patients.aggregate(pipe,allowDiskUse=True)
Run Code Online (Sandbox Code Playgroud)

我通过添加$project运算符来修复它,

但是,16MB即使我使用的文件仍然结束怎么$project办?

我能做什么 ?任何的想法 ?谢谢

pipe = [
    {"$project": {"birthday":1, "id":1}
    },
    {"$match": { "birthday":{"$gte":datetime.datetime(1987, 1, 1, 0, 0)} }
     }
    ]
res =db.patients.aggregate(pipe,allowDiskUse=True)
Run Code Online (Sandbox Code Playgroud)

例外

OperationFailure: command SON([('aggregate', 'patients'), ('pipeline', [{'$match': {'birthday': {'$gte': datetime.datetime(1987, 1, 1, 0, 0)}}}]), ('allowDiskUse', True)]) on namespace tw_insurance_security_development.$cmd failed: exception: aggregation result exceeds maximum document size (16MB)
Run Code Online (Sandbox Code Playgroud)

Fre*_*ung 28

默认情况下,聚合的结果将在单个BSON文档中返回给您,这是大小限制的来源.如果您需要返回更多,您可以:

  • 将结果输出到集合.你通过完成你的管道来做到这一点

    {"$ out":"some-collection-name"}

    然后你正常查询该集合(当你完成它时你需要自己删除它)

  • 通过指定useCursor=True何时调用aggregate 来将结果作为游标返回.

这两个选项都需要mongodb 2.6:如果你仍在运行mongodb 2.4,那么这只是聚合的基本限制.