Skip to content

Instantly share code, notes, and snippets.

@srleo
Last active December 17, 2015 23:19
Show Gist options
  • Save srleo/5688438 to your computer and use it in GitHub Desktop.
Save srleo/5688438 to your computer and use it in GitHub Desktop.
mongodb command line function to add a 'rand' field to every document in a collection.
function add_rand_field(collection) {
collection.find().forEach(function(data) {
collection.update({_id:data._id}, {
$set:{rand:Math.random()}
});
});
collection.ensureIndex({rand:1},{background:true})
}
add_rand_field(db.mycoll);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment