php - 为什么 mongo 聚合在 shell 中可以工作,但在 PHP 中却超过 16MB

标签 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 之间的巨大区别是什么?

如何解决这个问题?

最佳答案

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

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

关于php - 为什么 mongo 聚合在 shell 中可以工作,但在 PHP 中却超过 16MB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45233625/

相关文章:

php - Stripe 3 层支付订阅

PHP imagick 将图像从 CMYK 转换为 RGB 反转图像

java - 具有线性分布的 Group 和 Sum MongoDb 聚合框架

mongodb 填充平面文件结果?

mongodb - 如何在mongoDB中检索其值以特定字符结尾的文档

php - 在 PHP 中拼合 PDF?

php - 使用 PDO 对数组进行分页上限

node.js - 如何将 Mongoose 中的 bool 字段更新为相反的字段?

mongodb - 使用 CassandraDB/MongoDB 等进行 id 自动增量/序列仿真

mongodb - 如何从 atlas 备份文件中恢复 mongo?