为什么mongo聚合在shell中工作但在PHP中超过16MB

时间:2017-07-21 09:08:54

标签: php mongodb mongo-shell

我有一个非常庞大的mongo聚合命令。

db.container_product.aggregate([
    {"$unwind":"$product"},
    {"$group":{"_id":"$product","container_ids":{"$push":"$container_id"}}}
])

它产生了近5k组,但它们都是普通的整数。例如:

{ 
    "_id" : NumberInt(107058402), 
    "container_ids" : [
        NumberInt(107058409), 
        NumberInt(107058410), 
        NumberInt(107058411), 
        NumberInt(107058412), 
        NumberInt(107058413)
    ]
}

当我在shell(普通shell,还有Robomongo等)中运行它时它工作得很好。但是当我尝试从PHP运行它时,它会给我16MB的错误。

172.16.0.2:27017: exception: aggregation result exceeds maximum document size (16MB)

在这种情况下,PHP和shell之间的巨大差异是什么?

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:5)

Shell为aggregate command使用js包装器,隐式使用cursor,因此它可以返回任何大小的结果(每个文档的限制仍然适用16 MB)。您可以通过调用shell中的db.collection.aggregate来查看包装器的源代码。

在PHP MongoCollection::aggregate中直接调用命令返回单个文档,而不是光标。您需要改为使用MongoCollection::aggregateCursor