MongoDb aggregation

2019-07-22 17:59发布

How to write simial query using mongdoDb aggregation

select count(*) as ccount from a group by a.someField order by ccount desc limit 1

Using group function in mondoDb, I want to group my collection by specific key and return 1 row, which occurs more often

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);

2条回答
疯言疯语
2楼-- · 2019-07-22 18:15
啃猪蹄的小仙女
3楼-- · 2019-07-22 18:26

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

查看更多
登录 后发表回答