Created
February 27, 2012 06:26
-
-
Save tolitius/1921906 to your computer and use it in GitHub Desktop.
MongoDB Map/Reduce for Chariot Day
This file contains hidden or 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
db.nosqlrep.insert( { _id : 1, votes : ['riak', 'couchdb', 'cassandra'] } ); | |
db.nosqlrep.insert( { _id : 2, votes : ['redis', 'onetick', 'couchdb'] } ); | |
db.nosqlrep.insert( { _id : 3, votes : ['redis', 'riak', 'couchdb'] } ); | |
db.nosqlrep.insert( { _id : 4, votes : ['voltdb', 'mongodb', 'hazelcast'] } ); | |
db.nosqlrep.insert( { _id : 5, votes : ['hazelcast', 'riak', 'redis'] } ); | |
db.nosqlrep.insert( { _id : 6, votes : ['redis', 'cassandra', 'onetick'] } ); | |
db.nosqlrep.insert( { _id : 7, votes : [] } ); | |
print( "\ncommunity votes" ) | |
print( "---------------" ) | |
db.nosqlrep.find().forEach( printjson ) | |
map = function() { | |
this.votes.forEach( | |
function( z ) { | |
emit( z, { vote : 1 } ); | |
} | |
); | |
}; | |
reduce = function( key, values ) { | |
var total = 0; | |
for ( var i = 0; i < values.length; i++ ) | |
total += values[i].vote; | |
return { votes : total }; | |
}; | |
stats = db.nosqlrep.mapReduce( map, reduce, { out : "topdogs" } ); | |
print( "\nhere are the stats of the map/reduce:" ) | |
print( "-------------------------------------" ) | |
printjson( stats ); | |
print( "\nand the top dogs are..." ) | |
print( "-----------------------" ) | |
print( "// in here \t\t=> db.topdogs.find().sort( { value: -1 } )" ) | |
print( "// single DB rank \t=> db.nosqlrep.distinct( \"_id\", { votes: \"riak\" } ).length\n" ) |
This file contains hidden or 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
$ mongo --shell top-nosql-dogs.js | |
MongoDB shell version: 2.0.2 | |
connecting to: test | |
type "help" for help | |
community votes | |
--------------- | |
{ "_id" : 1, "votes" : [ "riak", "couchdb", "cassandra" ] } | |
{ "_id" : 2, "votes" : [ "redis", "onetick", "couchdb" ] } | |
{ "_id" : 3, "votes" : [ "redis", "riak", "couchdb" ] } | |
{ "_id" : 4, "votes" : [ "voltdb", "mongodb", "hazelcast" ] } | |
{ "_id" : 5, "votes" : [ "hazelcast", "riak", "redis" ] } | |
{ "_id" : 6, "votes" : [ "redis", "cassandra", "onetick" ] } | |
{ "_id" : 7, "votes" : [ ] } | |
here are the stats of the map/reduce: | |
------------------------------------- | |
{ | |
"result" : "topdogs", | |
"timeMillis" : 17, | |
"counts" : { | |
"input" : 7, | |
"emit" : 18, | |
"reduce" : 6, | |
"output" : 8 | |
}, | |
"ok" : 1, | |
} | |
and the top dogs are... | |
----------------------- | |
// in here => db.topdogs.find().sort( { value: -1 } ) | |
// single DB rank => db.nosqlrep.distinct( "_id", { votes: "riak" } ).length | |
> db.topdogs.find().sort( { value: -1 } ) | |
{ "_id" : "redis", "value" : { "votes" : 4 } } | |
{ "_id" : "couchdb", "value" : { "votes" : 3 } } | |
{ "_id" : "riak", "value" : { "votes" : 3 } } | |
{ "_id" : "cassandra", "value" : { "votes" : 2 } } | |
{ "_id" : "hazelcast", "value" : { "votes" : 2 } } | |
{ "_id" : "onetick", "value" : { "votes" : 2 } } | |
{ "_id" : "mongodb", "value" : { "vote" : 1 } } | |
{ "_id" : "voltdb", "value" : { "vote" : 1 } } | |
> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment