Skip to content

Instantly share code, notes, and snippets.

@jnm
Created July 15, 2016 14:46
Show Gist options
  • Save jnm/d445a3f87bba3fb30aa4a5aa5c10cbed to your computer and use it in GitHub Desktop.
Save jnm/d445a3f87bba3fb30aa4a5aa5c10cbed to your computer and use it in GitHub Desktop.
Get all the keys from all documents in a Mongo collection
// mapReduce idea credit: http://stackoverflow.com/a/2308036
db.instances.mapReduce(
function() {
// map
for(var key in this) {
emit(key, null);
}
},
function(key, values) {
// reduce
return null;
},
{out: {inline: 1}}
).results.map(function(value) {
// results are `_id`, `value` pairs, where `value` is always `null`
return value._id;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment