Skip to content

Instantly share code, notes, and snippets.

@dancrew32
Created November 8, 2013 07:54
Show Gist options
  • Save dancrew32/7367657 to your computer and use it in GitHub Desktop.
Save dancrew32/7367657 to your computer and use it in GitHub Desktop.
Mongo distinct keys map reduce
var collection = "your_collection"
var distinct_keys_map_reduce = db.runCommand({
"mapreduce" : collection,
"map" : function() {
for (var key in this) { emit(key, null); }
},
"reduce" : function(key, val) { return null; },
"out": collection + "_keys"
})
db[distinct_keys_map_reduce.keys].distinct("_id")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment