

{"id":48727,"date":"2019-02-09T10:30:05","date_gmt":"2019-02-09T05:00:05","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=48727"},"modified":"2021-05-09T13:07:17","modified_gmt":"2021-05-09T07:37:17","slug":"mongodb-mapreduce","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/mongodb-mapreduce\/","title":{"rendered":"MongoDB Mapreduce Tutorial &#8211; Real-time Example &amp; Commands"},"content":{"rendered":"<p>In the last tutorial we had learned about atomic operations and objectid in MongoDB. Today, we are going to discuss MongoDB mapreduce tutorial, in which we will know how mapreduce will work in MongDB. Along with this, we will learn MongoDB Mapreduce example and commands.<\/p>\n<h2>MongoDB Mapreduce<\/h2>\n<p><em>MongoDB Mapreduce is a data processing paradigm for constricting large amount of data into useful aggregated results. Which we can use for processing large number of data<\/em>.<\/p>\n<p>To understand it in a more better way, let&#8217;s take these two MongoDB Mapreduce example:<\/p>\n<div id=\"attachment_48768\" style=\"width: 909px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/02\/MongoDB-Mapreduce-Example.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-48768\" class=\"size-full wp-image-48768\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/02\/MongoDB-Mapreduce-Example.jpg\" alt=\"MongoDB Mapreduce Example\" width=\"899\" height=\"721\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/02\/MongoDB-Mapreduce-Example.jpg 899w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/02\/MongoDB-Mapreduce-Example-150x120.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/02\/MongoDB-Mapreduce-Example-300x241.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/02\/MongoDB-Mapreduce-Example-768x616.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/02\/MongoDB-Mapreduce-Example-520x417.jpg 520w\" sizes=\"auto, (max-width: 899px) 100vw, 899px\" \/><\/a><p id=\"caption-attachment-48768\" class=\"wp-caption-text\">MongoDB Mapreduce Example<\/p><\/div>\n<h4>MongoDB Mapreduce Example &#8211; 1<\/h4>\n<p>Here, map operation is performed to each input document. Map operation emits key-value pairs. For keys that have multiple values, MongoDB applies the reduce phase, which collects and condenses the aggregated data. Finally the result will be stored in collections.<\/p>\n<p>Optionally the output of reduce function may pass through finalize function to process the results of aggregation. All map-reduce functions in MongoDB are JavaScript and run with mongod process.<\/p>\n<p>They take documents of a single collection as input and perform any sorting and limiting before beginning the map stage.<\/p>\n<h4>MongoDB Mapreduce Example\u00a0&#8211; 2<\/h4>\n<p>Now we will consider another example on a collection named examples that contain documents of the following type:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">{\r\n    _id: ObjectId(\"50a8240b927d5d8b5891743c\"),\r\n    cust_id: \"a123\",\r\n    ord_date: new Date(\"Jan 04, 2019\"),\r\n    status: 'A',\r\n    price: 25,\r\n    items: [ { sku: \"m\", qty: 5, price: 2.5 },\r\n          { sku: \"n\", qty: 5, price: 2.5 } ]\r\n}<\/pre>\n<p>Define map function to process each input document:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">var mapFunction1 = function() {\r\n            emit(this.cust_id, this.price);\r\n          };<\/pre>\n<p>Define the corresponding reduce function with two arguments CustId and Prices:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">var reduceFunction1 = function(CustId, Prices) {\r\n                 return Array.sum(Prices);\r\n              };<\/pre>\n<p>Now perform map-reduce on all documents in the examples collection.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">db.examples.mapReduce(\r\n            mapFunction1,\r\n            reduceFunction1,\r\n            { out: \"map_reduce_example\" }\r\n           )<\/pre>\n<p>This operation will give the output collection as map_reduce_example.<\/p>\n<h4>How to calculate order and total quantity?<\/h4>\n<p>Now we will calculate order and total quantity with average quantity per item for the same example.<\/p>\n<p>Define map function to process each input document:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">var mapFunction2 = function() {\r\n           for (var idx = 0; idx &lt; this.items.length; idx++) {\r\n             var key = this.items[idx].sku;\r\n             var value = {\r\n                      count: 1,\r\n                      qty: this.items[idx].qty\r\n                    };\r\n           emit(key, value);\r\n           }\r\n        };<\/pre>\n<p>Define the corresponding reduce function with two arguments key and ObjVals:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">var reduceFunction2 = function(key, ObjVals) {\r\n          reducedVal = { count: 0, qty: 0 };\r\n          \r\n          for (var idx = 0; idx &lt; ObjVals.length; idx++) {\r\n             reducedVal.count += ObjVals[idx].count;\r\n             reducedVal.qty += ObjVals[idx].qty;\r\n          }\r\n          \r\n          return reducedVal;\r\n          };<\/pre>\n<p>Now define a finalize function with two arguments keys and reducedVal.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">var finalizeFunction2 = function (keys, reducedVal) {\r\n            reducedVal.avg = reducedVal.qty\/reducedVal.count;\r\n            return reducedVal;\r\n          };\r\n<\/pre>\n<p>After this we will perform map-reduce operation on examples collection.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">db.examples.mapReduce( mapFunction2,\r\n           reduceFunction2,\r\n           {\r\n            out: { merge: \"map_reduce_example\" },\r\n            query: { ord:\r\n                    { $gt: new Date('26\/01\/2019') }\r\n                  },\r\n            finalize: finalizeFunction2\r\n            }\r\n           )<\/pre>\n<h2>MongoDB MapReduce Command<\/h2>\n<p>Following are some commands, which can we use in map-reduce.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">db.collection.example( \r\n    function() {emit(key, value);}, \r\n\/\/Define map function\r\n    function(key,values) {return reduceFunction}, { \r\n\/\/Define reduce function\r\n    out: collection,\r\n    query: document,\r\n    sort: document,\r\n    limit: number\r\n  }\r\n)<\/pre>\n<p>Here in the above example it will query the collection, and then map the output documents to emit key-value pairs. We can perform this reduce operation on keys that are having multiple values.<\/p>\n<p>Here, we are going to discuss the MongoDB Mapreduce commands:<\/p>\n<ol>\n<li><strong>Map<\/strong>: It is a JavaScript function, which is use to generate key-value pairs.<\/li>\n<li><strong>Reduce<\/strong>: A JavaScript function use for grouping all the documents which have the same key.<\/li>\n<li><strong>Out<\/strong>: It specifies the location of map-reduce query output.<\/li>\n<li><strong>Query<\/strong>: We can use it to specify the selection criteria for selecting documents.<\/li>\n<li><strong>Sort<\/strong>: Use for specify sort criteria and optional commands.<\/li>\n<li><strong>Limit<\/strong>: It specifies the number of documents to be returned. It is an optional command to be used.<\/li>\n<\/ol>\n<h2>Summary<\/h2>\n<p>Hence, we have studied about map-reduce operation in MongoDB with real-time examples and commands. In our next tutorial, we will see RockMongo abd GridFS.<\/p>\n<p>If you have a query, feel free to ask in the comment session.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the last tutorial we had learned about atomic operations and objectid in MongoDB. Today, we are going to discuss MongoDB mapreduce tutorial, in which we will know how mapreduce will work in MongDB.&#46;&#46;&#46;<\/p>\n","protected":false},"author":6,"featured_media":48770,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38],"tags":[18761,18760,18765,18764,18762,18763],"class_list":["post-48727","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mongodb","tag-mapreduce-in-mongodb","tag-mongodb-mapreduce","tag-mongodb-mapreduce-commands","tag-mongodb-mapreduce-example","tag-mongodb-mapreduce-tutorial","tag-what-is-mongodb-mapreduce"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>MongoDB Mapreduce Tutorial - Real-time Example &amp; Commands - DataFlair<\/title>\n<meta name=\"description\" content=\"In this MongoDB mapreduce tutorial, you will learn how to use mapreduce operation in MongoDB with realtime example and commands: map, reduce, out, sort\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/mongodb-mapreduce\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"MongoDB Mapreduce Tutorial - Real-time Example &amp; Commands - DataFlair\" \/>\n<meta property=\"og:description\" content=\"In this MongoDB mapreduce tutorial, you will learn how to use mapreduce operation in MongoDB with realtime example and commands: map, reduce, out, sort\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/mongodb-mapreduce\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2019-02-09T05:00:05+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-05-09T07:37:17+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/02\/MongoDB-Mapreduce-01.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"MongoDB Mapreduce Tutorial - Real-time Example &amp; Commands - DataFlair","description":"In this MongoDB mapreduce tutorial, you will learn how to use mapreduce operation in MongoDB with realtime example and commands: map, reduce, out, sort","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/mongodb-mapreduce\/","og_locale":"en_US","og_type":"article","og_title":"MongoDB Mapreduce Tutorial - Real-time Example &amp; Commands - DataFlair","og_description":"In this MongoDB mapreduce tutorial, you will learn how to use mapreduce operation in MongoDB with realtime example and commands: map, reduce, out, sort","og_url":"https:\/\/data-flair.training\/blogs\/mongodb-mapreduce\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2019-02-09T05:00:05+00:00","article_modified_time":"2021-05-09T07:37:17+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/02\/MongoDB-Mapreduce-01.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/mongodb-mapreduce\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/mongodb-mapreduce\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89"},"headline":"MongoDB Mapreduce Tutorial &#8211; Real-time Example &amp; Commands","datePublished":"2019-02-09T05:00:05+00:00","dateModified":"2021-05-09T07:37:17+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/mongodb-mapreduce\/"},"wordCount":498,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/mongodb-mapreduce\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/02\/MongoDB-Mapreduce-01.jpg","keywords":["MapReduce in MongoDB","MongoDB MapReduce","MongoDB MapReduce Commands","MongoDB MapReduce Example","MongoDB MapReduce tutorial","What is MongoDB MapReduce"],"articleSection":["MongoDB Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/mongodb-mapreduce\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/mongodb-mapreduce\/","url":"https:\/\/data-flair.training\/blogs\/mongodb-mapreduce\/","name":"MongoDB Mapreduce Tutorial - Real-time Example &amp; Commands - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/mongodb-mapreduce\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/mongodb-mapreduce\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/02\/MongoDB-Mapreduce-01.jpg","datePublished":"2019-02-09T05:00:05+00:00","dateModified":"2021-05-09T07:37:17+00:00","description":"In this MongoDB mapreduce tutorial, you will learn how to use mapreduce operation in MongoDB with realtime example and commands: map, reduce, out, sort","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/mongodb-mapreduce\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/mongodb-mapreduce\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/mongodb-mapreduce\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/02\/MongoDB-Mapreduce-01.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/02\/MongoDB-Mapreduce-01.jpg","width":1200,"height":628,"caption":"MongoDB Mapreduce Tutorial - Real-time Example &amp; Commands"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/mongodb-mapreduce\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"MongoDB Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/mongodb\/"},{"@type":"ListItem","position":3,"name":"MongoDB Mapreduce Tutorial &#8211; Real-time Example &amp; Commands"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"The DataFlair Team provides industry-driven content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our expert educators focus on delivering value-packed, easy-to-follow resources for tech enthusiasts and professionals.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam2\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/48727","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=48727"}],"version-history":[{"count":3,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/48727\/revisions"}],"predecessor-version":[{"id":93178,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/48727\/revisions\/93178"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/48770"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=48727"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=48727"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=48727"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}