MapReduce applications in MongoDB

Source: Internet
Author: User

MapReduce can be used in MongoDB for complex aggregate queries.

Map and Reduce functions can be implemented using JavaScript.

You can use db. runCommand or mapReduce command to execute a MapReduce operation:

  1. Db. runCommand (

  2. {Mapreduce:<Collection>,

  3. Map:<Mapfunction>,

  4. Reduce:<Performancefunction>

  5. [, Query:<QueryFilter object>]

  6. [, Sort:<SortThe query. useful for optimization>]

  7. [, Limit:<NumberOf objects to return from collection>]

  8. [, Out:<Output-collectionName>]

  9. [, Keeptemp:<True| False>]

  10. [, Finalize:<Finalizefunction>]

  11. [, Scope:<ObjectWhere fields go into javascript global scope>]

  12. [, Verbose: true]

  13. }

  14. );

  15. # Or use a packaged Helper command

  16. Db. collection. mapReduce (mapfunction, performancefunction [, options]);

If no output is defined, a temporary collection is generated by default after it is executed. when the client is disconnected, the collection is automatically cleared.

A simple column has a collection of user_addr. The result is as follows:

  1. Db. user_addr.find ({'uid': 'test @ sohu.com '})

  2. {"_ Id": ObjectId ("4bbde0bf600ac3c3cc7245e3"), "Uid": "yangsong@sohu.com", "Al ":[

  3. {

  4. "Nn": "test-1 ",

  5. "Em": "test-1@sohu.com ",

  6. },

  7. {

  8. "Nn": "test-2 ",

  9. "Em": "test-2@sohu.com ",

  10. },

  11. {

  12. "Nn": "test-3 ",

  13. "Em": "test-3@sohu.com ",

  14. }

  15. ]}

Stores the contact information (Al) corresponding to a user (Uid). to query the number of contacts corresponding to each Em, create the following MapReduce

  1. M=Function(){

  2. For (index in this. Al ){

  3. Emit (this. Al [index]. Em, 1 );

  4. }

  5. }

  6. R=Function(K, vals ){

  7. VarSum=0;

  8. For (index in vals ){

  9. Sum + = vals [index];

  10. >}

  11. Return sum;

  12. }

  13. Res=Db. User_addr.mapReduce (m, r)

  14. {

  15. "Result": "tmp. mr. mapreduce_1272267853_1 ",

  16. "TimeMillis": 29,

  17. "Counts ":{

  18. "Input": 5,

  19. "Emit": 26,

  20. "Output": 26

  21. },

  22. "OK": 1,

  23. }

  24. Db [res. result]. find ()

The group function in MongoDB also needs to be implemented using MapReduce.

For example, group by uid and calculate the number of contacts for each uid.

  1. R=Function(Obj, prev ){

  2. Prev. sum + = obj. Al. length;

  3. }

  4. Db. user_addr.group ({key: {'uid': 1}, reduce: r, initial: {sum: 0 }})

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.