MongoDb聚合 [英] MongoDb aggregation
问题描述
如何使用mongdoDb聚合编写Simial查询
How to write simial query using mongdoDb aggregation
select count(*) as ccount from a group by a.someField order by ccount desc limit 1
使用mondoDb中的分组功能,我想按特定键对我的集合进行分组并返回1行,这种情况发生的频率更高
Using group function in mondoDb, I want to group my collection by specific key and return 1 row, which occurs more often
下面的Java代码,返回列表,该列表按字段someField进行分组,并返回元素的集合,其中每个元素都有2个字段,"someField"和"count"(出现次数),我想避免第二次在客户端进行迭代以找出最大数量
Code below in Java, return list, that was apply group by on field someField and return collection of elements, where every element has 2 fields, "someField" and "count" (number of occurrence) I would like to avoid second iteration on client side to find out maximum of counts
new GroupCommand(myCollection,
new BasicDBObject("someField ", true),
null,
new BasicDBObject("count", 0),
"function(key,val){ val.count++;}",
null);
推荐答案
MongoDB的group命令无法使用SQL的顺序或限制.当前开发版本(版本2.1)中的聚合框架提供了$ sort和$ limit运算符:
Nothing like SQL's order or limit is available for MongoDB's group command. The aggregation framework in the current development release (version 2.1) provides $sort and $limit operators:
http://www.mongodb.org/display/DOCS/Aggregation+Framework
这篇关于MongoDb聚合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!