MongoDB is a kind of nosql data, how can realize the group function in relational database.
Group
note:currently one must use Map/reduce instead the group () in Sharded MongoDB.
Group returns an array of grouped items. The command is similar to SQL ' s group by. The SQL statement
Select A,b,sum (c) csum from Coll where active=1 Group by a,b
Corresponds to the following in MongoDB:
Db.coll.group (
{key: {a:true, b:true},
cond: {active:1},
reduce:function (obj,prev) {prev.csum = OBJ.C; },
initial: {csum:0}
});
Note:the result is returned as a single Bson object and to this reason must to fairly the small–less keys, else you'll get a exception. For larger grouping operations without limits, please use map/reduce .
Group takes a single object parameter containing the following fields:key:Fields to group by. Reduce:the reduce function aggregates (reduces) the objects iterated. Typical operations of a reduce function include summing and counting. reduce takes two arguments:the Cument being iterated over and the aggregation counter object. In the example above arguments are named obj and prev. Initial:initial Value of Aggregation Nter object. Keyf:an optional function returning A "key object" to be used as the grouping key. Use this instead of key to specify the A key is not a single/multiple existing fields. Could is used to group by day of the week for example. Set in lieu of key. Cond:an optional condition that must is true for a row to is considered. This is essentially a find () query expression object. If NULL, the reduce function would run against all rows in the collection. Finalize:an Optional function to being run on each item of the result set just before the item is returned. Can either modify the item (e.g., add a average field given a count and a total) or return a replacement object (Returnin G A new object with just _id and average fields). Jstests/group3.js for examples.
Here's how I use the Mongovue tool to implement group
Here you can enter the key, which is the field of the group you want, if you do not give a {}. Conditions is a conditional filter. Status=30
Initial value is what you define the cumulative initial value, the value that is passed to it the first time the reduce method is called by each group, and in a group, the same cumulant is always used, and modifications to the cumulation are maintained.
Finalize
Using Finalizers (Finalizer)
Finalizers are used to minimize data from the database to the user, we look at a blog example, each blog has a few tags, we want to find out what is the most popular day tags. So we grouped by date, counting each tag:
JAVA for MONGODB:
Http://stackoverflow.com/questions/6013645/how-do-i-pass-parameters-to-mongodb-map-reduce-in-java