Databases Reference
In-Depth Information
... "initial" : {"time" : 0},
... "$reduce" : function(doc, prev) {
... if (doc.time > prev.time) {
... prev.price = doc.price;
... prev.time = doc.time;
... }},
... "condition" : {"day" : {"$gt" : "2010/09/30"}}
... }})
Some documentation refers to a "cond" or "q" key, both of which are
identical to the "condition" key (just less descriptive).
Now the command will return an array of 30 documents, each of which is a group.
Each group has the key on which the group was based (in this case, " day" : string )
and the final value of prev for that group. If some of the documents do not contain the
key, these will be grouped into a single group with a day : null element. You can
eliminate this group by adding "day" : {"$exists" : true} to the "condition" . The
group command also returns the total number of documents used and the number of
distinct values for "key" :
> db.runCommand({"group" : {...}})
{
"retval" :
[
{
"day" : "2010/10/04",
"time" : "Mon Oct 04 2010 11:28:39 GMT-0400 (EST)"
"price" : 4.27
},
...
],
"count" : 734,
"keys" : 30,
"ok" : 1
}
We explicitly set the "price" for each group, and the "time" was set by the initializer
and then updated. The "day" is included because the key being grouped by is included
by default in each "retval" embedded document. If you don't want to return this key,
you can use a finalizer to change the final accumulator document into anything, even
a nondocument (e.g., a number or string).
Using a Finalizer
Finalizers can be used to minimize the amount of data that needs to be transferred from
the database to the user, which is important, because the group command's output
needs to fit in a single database response. To demonstrate this, we'll take the example
 
Search WWH ::




Custom Search