I received a bunch of questions about using Map Reduce in MongoDB, so here I provide a bunch of examples. First I’ll cover Map Reduce using straight JavaScript. Then we’ll cover increasingly complex MongoDB Map Reduce queries. Then finally I’ll cover using Group and Distinct.
All of the code used is available after the video below. If you missed previous tutorials, you should watch my first MongoDB tutorial to start.
If you like videos like this, it helps when you tell Google with a click here [googleplusone]
Code & Transcript From the Video
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
---------- Map / Reduce ---------- 1. Map/Reduce allows you to split a large problem into smaller parts. The Map excepts data and transforms it into key / value pairs by applying multiple JavaScript functions. The Reduce excepts the key / value pairs and reduces them into a smaller aggregation of key / value pairs. 2. Why is Map useful? var students = [ {name : "Dale Cooper", class: "Calculus", tests: [30, 28, 45]}, {name : "Harry Truman", class: "Geometry", tests: [28, 26, 44]}, {name : "Shelly Johnson", class: "Calculus", tests: [27, 26, 43]}, {name : "Bobby Briggs", class: "College Algebra, tests: [20, 18, 35]"}, {name : "Donna Heyward", class: "Geometry", tests: [28, 28, 44]}, {name : "Audrey Horne", class: "College Algebra", tests: [22, 26, 44]}, {name : "James Hurley", class: "Calculus", tests: [20, 20, 38]}, {name : "Lucy Moran", class: "College Algebra", tests: [26, 24, 40]}, {name : "Tommy Hill", class: "College Algebra", tests: [30, 29, 46]}, {name : "Andy Brennan", class: "Geometry", tests: [20, 21, 38]} ]; var studNames = []; for( i=0; i < students.length; i++){ studNames.push(students[i].name) } studNames // With Map you can cycle the values through a callback function var studInfo = students.map(function(x) { return x.name + ' is in ' + x.class; }); studInfo Why is Reduce useful? var tests = [ {score: 30}, {score: 28}, {score: 45} ] // Reduce receives an object and a starting object // Sum is our final value and tests is the thing we iterate var testSum = tests.reduce(function(sum, tests){ return sum + tests.score; }, 0) testSum 3. Delete a database by switching to it and dropping it use testdb db.dropDatabase() 4. Add to classes collection db.classes.insert({ class : "Philosophy 101", startDate : new Date(2016, 1, 10), students : [ {fName : "Dale", lName : "Cooper", age : 42}, {fName : "Lucy", lName : "Moran", age : 35}, {fName : "Tommy", lName : "Hill", age : 44} ], cost : 1600, professor : "Paul Slugman", topics : "Socrates,Plato,Aristotle,Francis Bacon", book: { isbn: "1133612105", title: "Philosophy : A Text With Readings", price: 165.42 } }) db.classes.insert({ class : "College Algebra", startDate : new Date(2016, 1, 11), students : [ {fName : "Dale", lName : "Cooper", age : 42}, {fName : "Laura", lName : "Palmer", age : 22}, {fName : "Donna", lName : "Hayward", age : 21}, {fName : "Shelly", lName : "Johnson", age : 24} ], cost : 1500, professor : "Rhonda Smith", topics : "Rational Expressions,Linear Equations,Quadratic Equations", book: { isbn: "0321671791", title: "College Algebra", price: 179.40 } }) db.classes.insert({ class : "Astronomy 101", startDate : new Date(2016, 1, 11), students : [ {fName : "Bobby", lName : "Briggs", age : 21}, {fName : "Laura", lName : "Palmer", age : 22}, {fName : "Audrey", lName : "Horne", age : 20} ], cost : 1650, professor : "Paul Slugman", topics : "Sun,Mercury,Venus,Earth,Moon,Mars", book: { isbn: "0321815351", title: "Astronomy: Beginning Guide to Univ", price: 129.45 } }) db.classes.insert({ class : "Geology 101", startDate : new Date(2016, 1, 12), students : [ {fName : "Andy", lName : "Brennan", age : 36}, {fName : "Laura", lName : "Palmer", age : 22}, {fName : "Audrey", lName : "Horne", age : 20} ], cost : 1450, professor : "Alice Jones", topics : "Earth,Moon,Elements,Minerals", book: { isbn: "0321814061", title: "Earth : An Introduction to Physical Geology", price: 130.65 } }) db.classes.insert({ class : "Biology 101", startDate : new Date(2016, 1, 11), students : [ {fName : "Andy", lName : "Brennan", age : 36}, {fName : "James", lName : "Hurley", age : 25}, {fName : "Harry", lName : "Truman", age : 41} ], cost : 1550, professor : "Alice Jones", topics : "Earth,Cell,Energy,Genetics,DNA", book: { isbn: "0547219474", title: "Holt McDougal Biology", price: 104.30 } }) db.classes.insert({ class : "Chemistry 101", startDate : new Date(2016, 1, 13), students : [ {fName : "Bobby", lName : "Briggs", age : 21}, {fName : "Donna", lName : "Hayward", age : 21}, {fName : "Audrey", lName : "Horne", age : 20}, {fName : "James", lName : "Hurley", age : 25} ], cost : 1600, professor : "Alice Jones", topics : "Matter,Energy,Atom,Periodic Table", book: { isbn: "0547219474", title: "Chemistry : Matter and Change", price: 104.30 } }) 4. Map is called for every document in the collection. Map sends the total student name list to reduce. var mapFunc = function() { for(i = 0; i < this.students.length; i++){ var student = this.students[i]; // Emit gets 2 arguments being the key on which you want to group the // data and the data itself emit(student.fName + " " + student.lName, 1); } }; 5. Reduce is called by map and receives all the values for the given key. Then we add how many times the student name shows up in the collection of documents. var reduceFunc = function(student, values) { count = 0; for(i = 0; i < values.length; i++){ count += values[i]; } return count; }; 6. Define the map and reduce functions and where to output the results db.classes.mapReduce( mapFunc, reduceFunc, { out: "map_ex" } ) 7. "counts" : { "input" : 6, // 6 Classes sent to map "emit" : 20, // 20 total students input with duplicates "reduce" : 7, // 7 students are in more then 1 class "output" : 11 // Output all 11 students their classes } 8. Show the results db.map_ex.find() 9. Get all the professors var mapFunc2 = function(){ emit(this.professor,1); } 10. Sum the number of times they appear var reduceFunc2 = function(professor, count){ return Array.sum(count); } 11. With query only output data for Alice Jones db.classes.mapReduce( mapFunc2, reduceFunc2,{ query:{professor: "Alice Jones"}, out: "map_ex_2" } ) 12. Output every topic to be reduced var mapFunc3 = function(){ var topics = this.topics.split(','); for(i in topics){ emit(topics[i], 1); } } 13. Count up the total times each topic is covered across all classes var reduceFunc3 = function(key, values){ var count = 0; for(i in values){ count += values[i]; } return count; } db.classes.mapReduce( mapFunc3, reduceFunc3, { out: "map_ex_3" } ) 14. Send the cost to attend each of the professors classes to reduce var mapFunc4 = function(){ emit(this.professor, { count: 1, cost: this.cost }); } 15. Reduce down to professor and cost var reduceFunc4 = function(professor, values){ var value = { count: 0, cost: 0 }; for(i = 0; i < values.length; i++){ value.count += values[i].count; value.cost += values[i].cost; } return value; } 16. Condense the results down to show the average of all the professors classes var finalizeFunc4 = function(professor, value) { value.average = (value.cost / value.count); return value; } 17. db.classes.mapReduce( mapFunc4, reduceFunc4, { out: "map_ex_4", finalize: finalizeFunc4 } ) ---------- Random Database Commands ---------- 1. Distinct returns all distinct values for a given key db.runCommand({"distinct" : "classes", "key" : "professor"}) 2. Group allows you to perform complex aggregations and then separate the results into groups. Let's get every book title that costs more then $110 db.runCommand( { group: { // The collection ns: 'classes', // Keys to retrieve key: { 'book.price': 1, 'book.title': 1 }, // Condition that must be met cond: { 'book.price': { $gt: 110 } }, // Not reducing the results $reduce: function ( curr, result ) { }, // Stores the initial value the first time reduce is called initial: { } } } ) 3. Calculate the total cost of books for each class db.runCommand( { group: { ns: 'classes', key: { 'book.price': 1, 'book.title': 1 }, cond: { }, $reduce: function ( curr, result ) { result.total += (curr.book.price * curr.students.length); }, initial: { total : 0 } } } ) |
Leave a Reply