使用Mongoose的Mongo聚合框架提高了简单投影的文档限制异常

时间:2014-06-08 11:21:48

标签: node.js mongodb mongoose aggregation-framework

我有一个集合链接(下面的架构),有近500k条目。

 {
   url,
   title,
   owner,
   stars: { users: [{ name }]},
   createdAt
 }

我真的不明白为什么简单的聚合投影

var projection = { $project: { _id: 1, url: 1, title: 1, createdAt: 1 } }
Link.aggregate([projection]).exec(resultCallback);

提出

MongoError: exception: aggregation result exceeds maximum document size (16MB)
你可以解释一下吗?

我正在使用Mongoose(3.8.8)和Mongodb(2.6.0)

1 个答案:

答案 0 :(得分:1)

不确定MongoDB 2.6和on-wards中可用的选项是否在mongoose的.aggregate()方法实现中完全可用。但是应该有一个选项" hash / object"在管道参数之后可用。所以基本上:

var pipeline = [{ $project: { _id: 1, url: 1, title: 1, createdAt: 1 } }];
Link.aggregate(pipeline,{ cursor: true}, function(err,cursor) {

});

或者如果mongoose由于某种原因不喜欢它,那么只需获取原始节点驱动程序集合:

var pipeline = [{ $project: { _id: 1, url: 1, title: 1, createdAt: 1 } }];
Link.collection.aggregate(pipeline,{ cursor: true}, function(err,cursor) {

   if (err)
      throw err;

   // Do something with the cursor which is actually more akin to a node
   // stream interface with a basic .next() method and other helpers.

});

否则,由于您的输出正在炸毁16MB BSON限制,因此您始终可以输出到集合:

var pipeline = [
    { $project: { _id: 1, url: 1, title: 1, createdAt: 1 } },
    { $out: "newcollection" }
];

但是,由于您可能只是在测试,为什么不使用 $limit 管道阶段,直到您计算出其余的聚合:

var pipeline = [
    { $project: { _id: 1, url: 1, title: 1, createdAt: 1 } },
    { $limit: 50 }
];

因此有几种不同的处理方式。