{"id":13835,"date":"2014-10-25T03:18:11","date_gmt":"2014-10-24T21:48:11","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=13835"},"modified":"2014-10-25T03:18:11","modified_gmt":"2014-10-24T21:48:11","slug":"map-reduce-in-mongodb","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/map-reduce-in-mongodb\/","title":{"rendered":"Map-Reduce in MongoDB"},"content":{"rendered":"<p><a title=\"Documentation\" href=\"http:\/\/docs.mongodb.org\/manual\/core\/map-reduce\/\" target=\"_blank\">Map-reduce<\/a> is a data processing paradigm for condensing large volumes of data into useful aggregated results. Basically, in MongoDB map-reduce contains two JavaScript functions <strong><em>map<\/em><\/strong> and <strong><em>reduce<\/em><\/strong>.<\/p>\n<ul style=\"margin-bottom:25px\">\n<li>The <strong>map<\/strong> functions emits key-value pair.<\/li>\n<li>And for those keys which have multiple values <strong>reduce<\/strong> function collect and condenses aggregated data.<\/li>\n<\/ul>\n<p>Let&#8217;s discuss the problem from one of our project for better understanding of how and where we can use map-reduce.<\/p>\n<p><strong>The Problem<\/strong><br \/>\nWe need to find the number of patient visiting to Medical Practice (Hospital\/Clinic) by their\u00a0visit reason. The Visit document structure is:<\/p>\n<p>[sourcecode language=&#8221;java&#8221;]<br \/>\n{<br \/>\n\t&quot;_id&quot; : NumberLong(1),<br \/>\n        &quot;name&quot;:&quot;Amelia Watson&quot;,<br \/>\n\t&quot;visitReason&quot; : &quot;Broken leg, Fever&quot;<br \/>\n}<br \/>\n[\/sourcecode]<\/p>\n<p>Insert some documents into database.<\/p>\n<p>[sourcecode language=&#8221;java&#8221;]<br \/>\ndb.visit.insert({name:&quot;John Doe&quot;, visitReason:&quot;Cold, Cough&quot;});<br \/>\ndb.visit.insert({name: &quot;Amenda&quot;, visitReason:&quot;Regular Health Check-up&quot;});<br \/>\ndb.visit.insert({name: &quot;William&quot;, visitReason:&quot;Regular Health Check-up&quot;});<br \/>\ndb.visit.insert({name: &quot;Mark&quot;, visitReason:&quot;High Blood Pressure, Fever&quot;});<br \/>\ndb.visit.insert({name: &quot;Milly&quot;, visitReason:&quot;Bleeding gums&quot;});<br \/>\ndb.visit.insert({name: &quot;Bosco&quot;, visitReason:&quot;Food poisoning&quot;});<br \/>\ndb.visit.insert({name: &quot;Bart&quot;, visitReason:&quot;Migrane&quot;});<br \/>\ndb.visit.insert({name: &quot;Harry&quot;, visitReason:&quot;Headache&quot;});<br \/>\ndb.visit.insert({name: &quot;Nova&quot;, visitReason:&quot;Skin test&quot;});<br \/>\ndb.visit.insert({name: &quot;Sunny&quot;, visitReason:&quot;Cold&quot;});<br \/>\ndb.visit.insert({name: &quot;Leelo&quot;, visitReason:&quot;Fever&quot;});<br \/>\ndb.visit.insert({name: &quot;Martin&quot;, visitReason:&quot;Joint problem&quot;});<br \/>\n[\/sourcecode]<\/p>\n<p>Now we&#8217;ve populated some data in database, so let&#8217;s see how to write MongoDB query using map-reduce to get the solution for above problem. Below is the map function to emit data:<\/p>\n<p>[sourcecode language=&#8221;java&#8221;]<br \/>\nfunction() {<br \/>\n    if(this.visitReason != undefined){<br \/>\n      this.visitReason.split(&#8216;,&#8217;).forEach(function (v) {<br \/>\n        emit(v.trim(), 1);<br \/>\n      });<br \/>\n    }<br \/>\n}<br \/>\n[\/sourcecode]<\/p>\n<p>The above map will emit data for each <em>visit reason<\/em> as key with count <em>1<\/em> as value. Now, the reduce function will group all the <em>visit reason<\/em> (key-value pair) and calculate sum of all the count values of same\u00a0<em>visit reason<\/em>. Here is the reduce function:<\/p>\n<p>[sourcecode language=&#8221;java&#8221;]<br \/>\nfunction (key, values) {<br \/>\n    return Array.sum(values);<br \/>\n}<br \/>\n[\/sourcecode]<\/p>\n<p>The complete <strong>MongoDB<\/strong> query would be:<\/p>\n<p>[sourcecode language=&#8221;java&#8221;]<br \/>\ndb.visit.mapReduce(<br \/>\n  function() {<br \/>\n    if(this.visitReason != undefined){<br \/>\n      this.visitReason.split(&#8216;,&#8217;).forEach(function (v) {<br \/>\n        emit(v.trim(), 1);<br \/>\n      });<br \/>\n    }<br \/>\n  },<br \/>\n  function (key, values) {<br \/>\n    return Array.sum(values);<br \/>\n  },<br \/>\n  {out: &quot;dbStats&quot;}<br \/>\n).find();<br \/>\n[\/sourcecode]<br \/>\n<strong>Please note<\/strong>\u00a0that <code>dbStats<\/code>\u00a0 in <code>{out: \"dbStats\"}<\/code>\u00a0is the name of the collection \u00a0where the operation outputs the result. <\/p>\n<p>The output of above MongoDB query is:<br \/>\n[sourcecode language=&#8221;java&#8221;]<br \/>\n { &quot;_id&quot; : &quot;Bleeding gums&quot;, &quot;value&quot; : 15 }<br \/>\n { &quot;_id&quot; : &quot;Broken knee&quot;, &quot;value&quot; : 5 }<br \/>\n { &quot;_id&quot; : &quot;Broken leg&quot;, &quot;value&quot; : 12 }<br \/>\n { &quot;_id&quot; : &quot;Cough&quot;, &quot;value&quot; : 9 }<br \/>\n { &quot;_id&quot; : &quot;Fever with Pain&quot;, &quot;value&quot; : 8 }<br \/>\n { &quot;_id&quot; : &quot;Health Checkup&quot;, &quot;value&quot; : 18 }<br \/>\n { &quot;_id&quot; : &quot;High fever&quot;, &quot;value&quot; : 8 }<br \/>\n { &quot;_id&quot; : &quot;Low Blood Pressure&quot;, &quot;value&quot; : 1 }<br \/>\n[\/sourcecode]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Map-reduce is a data processing paradigm for condensing large volumes of data into useful aggregated results. Basically, in MongoDB map-reduce contains two JavaScript functions map and reduce. The map functions emits key-value pair. And for those keys which have multiple values reduce function collect and condenses aggregated data. Let&#8217;s discuss the problem from one of [&hellip;]<\/p>\n","protected":false},"author":57,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":1},"categories":[7],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/13835"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/57"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=13835"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/13835\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=13835"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=13835"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=13835"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}