Skip to content

Instantly share code, notes, and snippets.

@JanPaulEttles
Created December 8, 2011 15:47
Show Gist options
  • Select an option

  • Save JanPaulEttles/1447373 to your computer and use it in GitHub Desktop.

Select an option

Save JanPaulEttles/1447373 to your computer and use it in GitHub Desktop.
disctinctarray #1
use itemCollection
db.itemCollection.insert({
"id" : "1",
"somefield" : "blah",
"someotherfield" : "meh",
"notanotherfield" : "feh",
"items" : ["item0", "item1", "item2", "item2"]
})
db.itemCollection.insert({
"id" : "2",
"somefield" : "bleh",
"someotherfield" : "geh",
"notanotherfield" : "beh",
"items" : ["item0", "item1", "item2", "item1", "item0"]
})
db.itemCollection.insert({
"id" : "3",
"somefield" : "bluh",
"someotherfield" : "heh",
"notanotherfield" : "jeh",
"items" : ["item0", "item1", "item2", "item3", "item4"]
})
map = function() {
if (!this.items) {
return;
}
for (index in this.items) {
emit(this.items[index], 1);
}
}
reduce = function(key, values) {
var count = 0;
for (index in values) {
count += values[index];
}
return count;
}
db.runCommand( { mapreduce : "itemCollection", map : map , reduce : reduce , query : { "id" : "1"} , out : "items" } );
db.items.find().forEach(printjson);
{ "_id" : "item0", "value" : 1 }
{ "_id" : "item1", "value" : 1 }
{ "_id" : "item2", "value" : 2 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment