Created
February 25, 2016 16:57
-
-
Save bettiolo/932fee09eea57d27acb3 to your computer and use it in GitHub Desktop.
MongoDB
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
db.bettiolo_map_reduce.drop(); | |
db.bettiolo_map_reduce.insert( { id: 1, types : { a: 'foo', b: 'bar', c: 'baz' } } ); | |
db.bettiolo_map_reduce.insert( { id: 1, types : { a: 'foo', b: 'bar', c: 'baz' } } ); // duplicates | |
db.bettiolo_map_reduce.insert( { id: 2, types : { a: 'foo' } } ); | |
db.bettiolo_map_reduce.insert( { id: 3, types : { b: 'bar' } } ); | |
db.bettiolo_map_reduce.insert( { id: 4 } ); | |
db.bettiolo_map_reduce.insert( { id: 5, types: {} } ); | |
var map = function () { | |
for (var type in this.types) { | |
emit(type, this.id); | |
} | |
}; | |
var reduce = function (key, values) { | |
return Array.unique(values); | |
}; | |
var mr = db.bettiolo_map_reduce.mapReduce(map, reduce, { | |
out: { "inline" : 1}, | |
query: { types: { $ne: null } }, | |
jsMode: true, | |
}); | |
db.bettiolo_map_reduce.drop(); | |
mr.results |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment