您的位置:首页 > 数据库 > Mongodb

MongoDB 2.6 兼容性改变 -- db.collection.aggregate()改变

2015-06-12 19:11 736 查看
MongoDB 2.6 兼容性改变 -- db.collection.aggregate()改变

描述

db.collection.aggregate()方法在mongo shell中,默认返回结果集的游标。这个修改使得聚合管道返回任何大小的结果集,需要游标遍历来访问结果集。例如:
var myCursor = db.orders.aggregate( [
{
$group: {
_id: "$cust_id",
total: { $sum: "$price" }
}
}
] );
myCursor.forEach( function(x) { printjson (x); } );

之前的版本返回带有字段result的单一文档,它包含了结果集的一个数组,受限于BSON文档大小限制。在MongoDB之前的版本访问结果集需要访问result字段,并遍历数组。例如:
var returnedDoc = db.orders.aggregate( [
{
$group: {
_id: "$cust_id",
total: { $sum: "$price" }
}
}
] );
var myArray = returnedDoc.result; // access the result field
myArray.forEach( function(x) { printjson (x); } );

解决方案

修改脚本,当前期待db.collection.aggregate()返回一个文档带result数组字段,替换为处理游标。

可以参考

聚合增强
db.collection.aggregate()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: