Last active
December 17, 2015 23:19
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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