Skip to content

Instantly share code, notes, and snippets.

@rcaloras
Last active June 9, 2022 11:22
Show Gist options
  • Save rcaloras/6bf5a80c726ea3230322 to your computer and use it in GitHub Desktop.
Save rcaloras/6bf5a80c726ea3230322 to your computer and use it in GitHub Desktop.
Add JUUID to every Mongo document that doesn't contain one
// Guid generator function based on stackoverflow
// http://stackoverflow.com/a/2117523/1282124
function guid(){
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
}
// Get a collection and add a uuid to each document
// in that collection doesn't have one
function addUUIDToCollection(collectionName) {
var col = db.getCollection(collectionName);
col.find({ uuid: { $exists: false } }).forEach( function (doc) { doc["uuid"] = JUUID(guid()); col.save(doc); });
}
@asgs
Copy link

asgs commented Jun 9, 2022

where is JUUID defined?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment