Php mongo db组顺序
我希望mongodb查询此sql查询的:Php mongo db组顺序,php,mongodb,group-by,mongo-collection,nosql,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::组 有人能帮我吗?谢谢 select a,b,sum(c) csum from coll where active=1 group by a,b 分别是 db.coll.group( {key: { a:true, b:
select x,y,message,foo from messege where x=1 and y=1 group by x,y order by _id DESC
但是:
MongoCollection::组
有人能帮我吗?谢谢
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 }
});
您不能对从组中获得的结果进行排序,您可以对desc-1使用sort for find,对asc使用1:.sort({“\u id”:-1})desc
选中此复选框我被查询的“order by”部分弄糊涂了-因为您是按单个值(x和y均为1)分组的,所以只能有一个结果-您确定这是您真正想要的SQL吗?这里没有聚合。@AsyaKamsky是的,我在MySQL上测试了它。请发布结果好吗?我不明白这怎么可能返回多行。当排序依据id不是输出的一部分时,如何添加该id?在何处添加“.sort()”。组返回对象和数组,而不是游标。我想找到适合我的最后一张唱片。不排序descWell您无法对使用group得到的结果进行排序,您可以在客户端或使用mapReduce进行排序您完全可以对使用group得到的结果进行排序-您只是不能按不存在的键对它们进行排序,这与SQL相同