MongoDB の MapReduce
In the study Hadoop MapReduce been exposed, and it is a very sophisticated computational model, the bulk of the work (ie data) decomposition (MAP mapping) executed, the final results will be merged into the final Reduce.
MongoDB provides a very flexible MapReduce
The basic syntax of MapReduce
>db.collection.mapReduce(
function() {emit(key,value);}, //map 函数
function(key,values) {return reduceFunction}, //reduce 函数
{
out: collection,
query: document,
sort: document,
limit: number
}
)
Use Map Reduce the need to achieve two functions, Map and Reduce, Map calls emit (key, value), traverse the collection (ie table) all records inside, and the key and the value passed to reduce processing function
Description :
- map: mapping function (sequence generating key pairs, so-called parameter reduce)
- reduce statistical functions, the task is to reduce the function key-values become key_value (the Values array into a single value)
- out storage statistics collection (if not specified, will use the temporary collection will be deleted after the client disconnects)
- query is a filter condition (not every document will trigger the map function)
- sort and limit
Do a little test, insert a bit of data entered, try
Data collection is shown below in
the article published selected by user_name packet, use the find function display
map function and a reduce function can use JS to achieve, it is very convenient