In addition to the basic query functionality, MONGODB also has powerful aggregation tools, including: count (), distinct (), group (), MapReduce.
counting function count
Count is the simplest aggregation tool that returns the number of documents:
>db.user.count ()//Returns the number of collection user
When you pass a query document, you calculate the number of query results:
>db.user.count ({"Age": {"$lt": 20}})//Returns the number of users younger than 20 years
This function is necessary for the total number of pagination
go to heavy function distinct
The distinct function is used to find out all the different values for a given key. You must specify the collection and key when you use it:
>db.runcommand ({"distinct": "User", "Key": "Age"})//Get different values for the age key
Sometimes you need to get all the different keys in the collection, and you'll need to write the mapreduce yourself, with no such function built in.
Values returns all the different values of the key corresponding to the array form.
Stats returns some indicators of the distinct process.
N: The number of returned collections,
nscanned: Number of scanned documents,
Timems: Elapsed time (in milliseconds),
Cursor: Index Used (basiccursor: no index, btreecursor)
Grouping Function Group
MongoDB divides a collection into groups based on the key grouped by, and then aggregates the documents within each group.
>db.runcommand ({"group": {
"NS": "User",
"Key": "Day",
"Initial": {"Time": 0},
"$reduce": function (Doc,prev) {
if (doc.time>prev.time) {
Prev.price = Doc.price;
Prev.time = Doc.time;
}
},
"condition": {"Day": {"$gt": "2014/12/21"}}
}})
"NS": "User": Specifies that the set of groupings is user
"Key": "Day": Specifies the key on which the document is grouped
"Initial": {"Time": 0} The duration of each set of reduce function calls is passed as the initial document to the subsequent procedure.
"$reduce": Each document corresponds to this call once. The system passes two parameters: the current document and the accumulator document.
"condition": Specifying conditions
using the completion device: Finalizer
The completion finalizer is used to streamline the data that the database passes to the user, and the output of the group command must be placed in a single database response.
>db.runcommand ({"group": {
"NS": "POST",
"Key": "{" tags ": true}",//equivalent to "key": "Tags"
"Initial": {"tags": {}},
"$reduce": function (Doc,prev) {
For (i in Doc.tags) {
if (Doc.tags[i] in prev.tags) {
prev.tags[doc.tags[i]]++;
}else{
Prev.tags[doc.tags[i]]=1;
},
"Finalize": function (prev) {
var mostpopular = 0;
For (i in Prev.tags) {
if (prev.tags[i]>mostpopular) {
Prev.tag=i;
Mostpopular=prev.tags[i];
}
}
Detete Prev.tags
}
}}})
The $reduce processed results are then processed and returned to the client.
use the function as a key
Use "$keyf" instead of "key" when defining grouping functions:
"$keyf": function (x) {return x.category.tolowercase ();}
...
MapReduce
The things that count, distict, group can do, and MapReduce can do. The basic use of MapReduce is as follows:
>mr=db.runcommand ({"MapReduce": "User", "map": Map, "Reduce": reduce})
"MapReduce": which set to work with
"Map": the map function, which can be defined before the above command
"Reduce": the reduce function, which can be defined before the above command
The map function returns the value to be processed using the function emit, which represents a reference to the current document:
>map=function () {
for (var key in this) {
Emit (key,{"Count": 1});
}};
Reduce can handle the various combinations of documents returned by emit and other reduce structures:
>reduce=function (key,emits) {
Total = 0;
for (Var I in emits) {
Total+=emits[i].count;
}
return {"Count": total};
}
The return document for the MapReduce function is similar to the following:
Result: The collection name that holds the MapReduce result, and is automatically deleted after the MapReduce connection is closed for a temporary collection.
Timemillis: How long the operation takes, in milliseconds
Input: The number of documents that occurred to the map function
Emit: Number of times emit is called in the map function
Output: The number of documents created in the result collection.
other optional keys for MapReduce
The MapReduce command, in addition to the required keys: MapReduce, map, and reduce, has the following optional keys:
Finalize: Further processing of the output results of reduce
Keeptemp: The temporary collection is saved when the connection is closed.
Output: The name of the result set, which is implied by the keeptemp:true.
Query: Filters the document with the specified criteria before it is sent to the map function.
Sort: Sorts the documents before sending them to map.
Limit: The maximum number of documents to be sent to the map function.
Scope:javascript the variable (variable name: value) to be used in the code.
VERBOSE: Whether to generate more detailed server logs.
MongoDB Learning Note-05 Aggregations