Today to share with you the more interesting knowledge of MongoDB, mainly include: aggregation, cursors.
One: Aggregation
Common aggregation operations are the same as SQL Server: Count,distinct,group,mapreduce.
<1> Count
Count is the simplest, easiest, and most commonly used aggregation tool, and its use is exactly the same as the count used in our C #.
<2> DISTINCT
This operation is believed that everyone is very familiar with, the designated who, who can not repeat, directly above the figure.
<3> Group
It's a little complicated to do group operations in MongoDB, but it's a bit more familiar with the group in SQL Server.
Can see clearly, in fact, group operations essentially formed a "k-v" model, like C # in the dictionary, good, with this thinking,
Let's take a look at how to use group.
The following example is a group operation according to age, and value is the name of the corresponding age. Here are some of these parameters:
Key: This is the group key, we are here to age group.
Initial: Each group shares an "initialization function", paying special attention to: Each group, such as the age=20 value of this list, shares a
Initial function, age=22 also shares a initial function.
$reduce: The first parameter of this function is the current document object, and the second is the cumulative object of the last function operation, the first time
is {"Perosn" in initial: []}. How many documents $reduce will be called.
Looking at the results above, it is not a bit of a feeling that we see the corresponding name staff through age, but sometimes we may have the following requirements:
①: Want to filter out age>25 some people.
②: Sometimes there are too many people in the person array, I want to add a count attribute to indicate.
For the above requirements, the group is still good to do, because the group has so two optional parameters: Condition and finalize.
Condition: This is the filtration condition.
Finalize: This is a function that triggers this method after each set of documents has been executed, so adding count to each set of collections is its life.
<4> MapReduce
This is the most complex aggregation function, but the more complex, the more complex the more flexible.
MapReduce is actually a programming model, used in distributed computing, which has a "map" function, a "reduce" function.
①map:
This is called the mapping function, which will call emit (Key,value), and the collection will be grouped according to the key you specify.
②reduce:
This is called a simplified function that groups the data after the map grouping to simplify, noting that the key in reduce (Key,value) is
The Key,vlaue in emit is the collection of emit (value) after the emit grouping, which is the array of many {"Count": 1}.
③mapreduce:
This is the last function to execute, with the arguments map,reduce and some optional arguments. The detailed picture can be seen:
From the diagram we can see the following information:
Result: "Stored set name";
Input: The number of incoming documents.
Emit: The number of times this function was invoked.
Reduce: The number of times this function was invoked.
Output: Returns the number of documents last.
Finally, let's take a look at the "Collecton" collection, grouped by name.
Two: Cursors
The MongoDB inside of the cursor is somewhat similar to what we say in C # where deferred execution, such as:
var list=db.person.find ();
For such an operation, the list actually does not get the document in person, but rather declares a "query structure", which is passed when we need it.
For or next () load it all at once, then let the cursor read line by row, and when we've enumerated it, the cursor is destroyed, and after we get it through the list,
found no data returned.
Of course, our "query construction" can also make a complex point, such as pagination, sorting can be added.
var single=db.person.find (). Sort ({"Name", 1}). Skip (2). Limit (2);
So this "query construction" can be implemented when we need to execute, greatly increasing the unnecessary costs.