MongoDB 2.6 兼容性改变 -- db.collection.aggregate()改变
更新:HHH   时间:2023-1-7


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()

返回MongoDB数据库教程...