Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
MongoDB
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
You can’t perform that action at this time.