Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use MapReduce in MongoDB

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article shows you how to use MapReduce in MongoDB. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

String map = @ "function () {var view = this;emit (view.activity, {pv: 1}); string reduce = @" function (key, values) {var result = {pv: 0}; values.forEach (function (value) {result.pv + = value.pv;}); return result;} "; string finalize = @" function (key, value) {return value;} "

MapReduce

MapReduce in MongoDB can be used to implement more complex aggregation commands. Using MapReduce mainly implements two functions: the map function and the reduce function, the map function is used to generate the sequence of key-value pairs, and the result of the map function is used as the parameter of the reduce function. Further statistics can be done in the reduce function. For example, my dataset is as follows:

{"_ id": ObjectId ("59fa71d71fd59c3b2cd908d7"), "name": "Lu Xun", "book": "Scream", "price": 38.0," publisher ":" people's Literature Publishing House "{" _ id ": ObjectId (" 59fa71d71fd59c3b2cd908d8 ")," name ":" Cao Xueqin "," book ":" A Dream of Red Mansions "," price ": 22.0 "publisher": "people's Literature Publishing House"} {"_ id": ObjectId ("59fa71d71fd59c3b2cd908d9"), "name": "Qian Zhongshu", "book": "selected Notes of Song Poems", "price": 99.0, "publisher": "people's Literature Publishing House" {"_ id": ObjectId ("59fa71d71fd59c3b2cd908da"), "name": "Qian Zhongshu", "book": "talking about Art Records", "price": 66.0 "publisher": "Sanlian Bookstore"} {"_ id": ObjectId ("59fa71d71fd59c3b2cd908db"), "name": "Lu Xun", "book": "hesitation", "price": 55.0, "publisher": "Huacheng Press"}

If I want to check the total price of each author's book, do the following:

Var map=function () {emit (this.name,this.price)} var reduce=function (key,value) {return Array.sum (value)} var options= {out: "totalPrice"} db.sang_books.mapReduce (map,reduce,options); db.totalPrice.find ()

The emit function is mainly used to implement grouping, receiving two parameters, the first parameter represents the field of the grouping, the second parameter represents the data to be counted, reduce does the specific data processing operation, and receives two parameters corresponding to the two parameters of the emit method. Here, the sum function in Array is used to self-add the price field, and the set of output results is defined in options. Then we will query the data in this set. By default, this collection is retained even after the database is restarted, and the data in the collection is retained.

The query results are as follows:

{"_ id": "Cao Xueqin", "value": 22.0} {"_ id": "Qian Zhongshu", "value": 165.0} {"_ id": "Lu Xun", "value": 93.0}

For example, I would like to find out how many books each author has published, as follows:

Var map=function () {emit (this.name,1)} var reduce=function (key,value) {return Array.sum (value)} var options= {out: "bookNum"} db.sang_books.mapReduce (map,reduce,options); db.bookNum.find ()

The query results are as follows:

{"_ id": "Cao Xueqin", "value": 1.0} {"_ id": "Qian Zhongshu", "value": 2.0} {"_ id": "Lu Xun", "value": 2.0}

List each author's book as follows:

Var map=function () {emit (this.name,this.book)} var reduce=function (key,value) {return value.join (',)} var options= {out: "books"} db.sang_books.mapReduce (map,reduce,options); db.books.find ()

The results are as follows:

{"_ id": "Cao Xueqin", "value": "A Dream of Red Mansions"} {"_ id": "Qian Zhongshu", "value": "Notes on selected Poems of the Song Dynasty"} {"_ id": "Lu Xun", "value": "Scream, hesitation"}

For example, inquire about books sold for more than ¥40 per person:

Var map=function () {emit (this.name,this.book)} var reduce=function (key,value) {return value.join (',)} var options= {query: {price: {$gt:40}}, out: "books"} db.sang_books.mapReduce (map,reduce,options); db.books.find ()

Query means to filter the found collection.

The results are as follows:

{"_ id": "Qian Zhongshu", "value": "Notes on selected Poems of the Song Dynasty"} {"_ id": "Lu Xun", "value": "hesitation"}

RunCommand implementation

We can also use the runCommand command to execute MapReduce. The format is as follows:

Db.runCommand ({mapReduce:, map:, reduce:, finalize:, out:, sort:, limit:, scope:, jsMode:, verbose:, bypassDocumentValidation:, collation:})

The meaning is as follows:

Parameter meaning mapReduce represents the set to operate mapmap function reducereduce function finalize final processing function out output set query filters the results sort sorts the results the number of results returned by limit sets parameter values, the values set here can be seen in the map, reduce, finalize functions whether jsMode converts the intermediate data executed by map from javascript objects to BSON objects. The default is whether falseverbose displays detailed time statistics and whether bypassDocumentValidation bypasses documents to verify some other proofs in collation

The following operations are performed to perform the MapReduce operation and return the number of entries for the statistical set limit, and then perform the statistical operation after limiting the number of entries returned, as follows:

Var map=function () {emit (this.name,this.book)} var reduce=function (key,value) {return value.join (',')} db.runCommand ({mapreduce:'sang_books',map,reduce,out: "books", limit:4,verbose:true}) db.books.find ()

The implementation results are as follows:

{"_ id": "Cao Xueqin", "value": "A Dream of Red Mansions"} {"_ id": "Qian Zhongshu", "value": "Notes on selected Poems of the Song Dynasty"} {"_ id": "Lu Xun", "value": "Scream"}

Friends see that one of Lu Xun's books is missing because limit first limits the number of entries returned by the collection, and then performs statistical operations.

The finalize operation represents the final processing function, as follows:

Var F1 = function (key,reduceValue) {var obj= {}; obj.author=key;obj.books=reduceValue; return obj} var map=function () {emit (this.name,this.book)} var reduce=function (key,value) {return value.join (',')} db.runCommand ({mapreduce:'sang_books',map,reduce,out: "books", finalize:f1}) db.books.find ()

F1 the first parameter key represents the first parameter in emit, and the second parameter represents the execution result of reduce. We can reprocess this result in F1. The result is as follows:

{"_ id": "Cao Xueqin", "value": {"author": "Cao Xueqin", "books": "A Dream of Red Mansions"} {"_ id": "Qian Zhongshu", "value": {"author": "Qian Zhongshu", "books": "Notes on selected Poems of the Song Dynasty"} {"_ id": "Lu Xun" "value": {"author": "Lu Xun", "books": "Scream, hesitation"}}

Scope can be used to define a variable that is visible in map, reduce, and finalize, as follows:

Var F1 = function (key,reduceValue) {var obj= {}; obj.author=key;obj.books=reduceValue;obj.sang=sang; return obj} var map=function () {emit (this.name,this.book)} var reduce=function (key,value) {return value.join (',--'+ sang+'--,')} db.runCommand ({mapreduce:'sang_books',map,reduce,out: "books", finalize:f1,scope: {sang: ""}}) db.books.find ()

The implementation results are as follows:

{"_ id": "Cao Xueqin", "value": {"author": "Cao Xueqin", "books": "A Dream of Red Mansions", "sang": ""} {"_ id": "Qian Zhongshu", "value": {"author": "Qian Zhongshu", "books": selected Notes on Poems of the Song Dynasty "sang": ""} {"_ id": "Lu Xun", "value": {"author": "Lu Xun", "books": "Scream,-- --, hesitation", "sang": ""}} this is how MapReduce is used in MongoDB Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report