mongodb group-order

时间:2012-11-24 15:19:47

标签: php mongodb group-by mongo-collection nosql

我想mongodb查询这个sql查询:

select x,y,message,foo from messege where x=1 and y=1 group by x,y order by _id DESC

但是:

MongoCollection ::组

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

对于此

select a,b,sum(c) csum from coll where active=1 group by a,b

各自是

db.coll.group(
           {key: { a:true, b:true },
            cond: { active:1 },
            reduce: function(obj,prev) { prev.csum += obj.c; },
            initial: { csum: 0 }
            });

您无法对从组中获得的结果进行排序,您可以使用sort for find for this for desc -1,1 for asc:.sort({“_ id”: - 1})desc

检查此http://www.mongodb.org/display/DOCS/Aggregation#Aggregation-Group