MongoDb aggregation

帅比萌擦擦* 提交于 2019-12-11 02:46:30

问题


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

回答1:


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




回答2:


In mongoDB 3 you can use limit and skip:

http://docs.mongodb.org/manual/reference/operator/aggregation/



来源:https://stackoverflow.com/questions/10387592/mongodb-aggregation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!