Mongodb group and sort

前端 未结 8 2178
隐瞒了意图╮
隐瞒了意图╮ 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条回答
  •  盖世英雄少女心
    2020-12-09 16:28

    I built up a histogram and what I did with version 2.2.2 was:

    answer = db.coll.group(...)
    db.histo.insert(answer)
    db.histo.find().sort({ field: 1 })
    

    At this point, if you don't need it, just db.histo.drop().

    You can also avoid the variable and do:

    db.histo.insert(db.coll.group(...))
    db.histo.ensureIndex({ field: 1 })
    

提交回复
热议问题