Mongodb group and sort

前端 未结 8 2139
隐瞒了意图╮
隐瞒了意图╮ 2020-12-09 15:31

How can I translate the following Sql query for Mongo?:

select a,b,sum(c) csum from coll wh         


        
8条回答
  •  萌比男神i
    2020-12-09 16:25

    Using the aggregate framework, you can do the following:

    db.coll.aggregate({ 
        $group: { 
            _id: "$a", 
            countA: { $sum: 1}, 
            sumC:{ $sum: "$c"}, 
        },
        $sort:{a:1}
    });
    

    However, if you have too much data you may get the following error message:

    {
        "errmsg" : "exception: aggregation result exceeds maximum document size (16MB)",
        "code" : 16389,
        "ok" : 0
    }
    

    See more about SQL to Mongo translation here: http://docs.mongodb.org/manual/reference/sql-aggregation-comparison/

提交回复
热议问题