Mongodb group and sort

前端 未结 8 1995
隐瞒了意图╮
隐瞒了意图╮ 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 })
    
    0 讨论(0)
  • 2020-12-09 16:29
    db.coll.group(
           {key: { a:true, b:true },
            cond: { active:1 },
            reduce: function(obj,prev) { prev.csum += obj.c; },
            initial: { csum: 0 }
            });
    

    You can execute it in MongoDB

    0 讨论(0)
提交回复
热议问题