Created
April 29, 2011 03:37
-
-
Save anonymous/947786 to your computer and use it in GitHub Desktop.
MongoDB map reduce example 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// suggested shell cmd line to run this: | |
// | |
// mongo --shell example2.js | |
// | |
// Note: the { out : … } parameter is for mongodb 1.8+ | |
db.things.insert( { _id : 1, tags : ['dog', 'cat'] } ); | |
db.things.insert( { _id : 2, tags : ['cat'] } ); | |
db.things.insert( { _id : 3, tags : ['mouse', 'cat', 'dog'] } ); | |
db.things.insert( { _id : 4, tags : [] } ); | |
// map function | |
m = function(){ | |
this.tags.forEach( | |
function(z){ | |
emit( z , { count : 1 } ); | |
} | |
); | |
}; | |
// reduce function | |
r = function( key , values ){ | |
var total = 0; | |
for ( var i=0; i<values.length; i++ ) | |
total += values[i].count; | |
return { count : total }; | |
}; | |
res = db.things.mapReduce(m, r, { out : "myoutput" } ); | |
printjson(res); | |
print("try db.myoutput.find()"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment