mongoose $sum

版权声明:转载请评论留言 https://blog.csdn.net/solocao/article/details/86373212
_id:...
Segment:[
    { field: "S1", value: 1 },
    { field: "S2", value: 5 },
    { field: "Sn", value: 10 },
]

查询方式

db.collection.aggregate( [
    { $unwind: "$Segment" },
    { $group: {
        _id: '$_id', 
        sum: { $sum: '$Segment.value' } 
    } } 
] );

结果


{
    "result" : [
        {
            "_id" : ObjectId("51e4772e13573be11ac2ca6f"),
            "sum" : 16
        }
    ],
    "ok" : 1
}

好的教程 https://www.compose.com/articles/aggregations-in-mongodb-by-example/

猜你喜欢

转载自blog.csdn.net/solocao/article/details/86373212
sum