Last active
June 9, 2022 11:22
-
-
Save rcaloras/6bf5a80c726ea3230322 to your computer and use it in GitHub Desktop.
Add JUUID to every Mongo document that doesn't contain one
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
// 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); }); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
where is
JUUID
defined?