MongoDB (Map-reduce)

Source: Internet
Author: User

The following example shows a mapreduce example of mongoose, referring to the Mongoose official website.

Basic concepts:

map Functionaccepts a key-value pair (Key-value pair), which produces a set of intermediate key-value pairs. The MapReduce framework passes the same value of the middle key value generated by the map function to a reduce function. Reduce functionaccepts a key, together with a related set of values, that merges the set of values to produce a smaller set of values (usually with only one or 0 values).
    • Define a collection:
var user    new  Schema ({        username:{type:string},         password:{type:string},} )
    • Mapreduce:
Exports.test =function(){        varuser = require ('.. /dao/user '); varModel =User.model; varoption = {}; //output is _id:name, value:1Option.map =function() {Emit ( This. Username, 1) }    //Pass the same key value to the same reduce function, where k is _id:name and Vals is an array of value    //when there are no more than one key-value pair, the reduce function is not calledOption.reduce =function(k, vals) {returnvals.length} model.mapreduce (O,function(err, results) {Console.log (results)});}
    • Output Result:
// There are three records in the database, a user name is Hello, and two user name is Test [{_id: ' Hello ', value:1}, {_id: ' Test ', value:2}]

MongoDB (Map-reduce)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.