Skip to content

Instantly share code, notes, and snippets.

@saggiyogesh
Created September 7, 2018 06:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saggiyogesh/d6ac7083570604eb57f63400d6a80af1 to your computer and use it in GitHub Desktop.
Save saggiyogesh/d6ac7083570604eb57f63400d6a80af1 to your computer and use it in GitHub Desktop.
Remove null, undefined from doc in mongodb. [Terminal code]
function getNullKeysRecursively(doc, keyName, nullKeys) {
for (var item_property in doc) {
// SKIP BASE-CLASS STUFF
if (!doc.hasOwnProperty(item_property)) continue;
// SKIP ID FIELD
if (item_property === '_id') continue;
// FULL KEY NAME (FOR SUB-DOCUMENTS)
var fullKeyName;
if (keyName) fullKeyName = keyName + '.' + item_property;
else fullKeyName = item_property;
// DEBUGGING
// print("fullKeyName: " + fullKeyName);
// NULL FIELDS - MODIFY THIS BLOCK TO ADD CONSTRAINTS
if (doc[item_property] === null || doc[item_property] === undefined) nullKeys[fullKeyName] = 1;
// RECURSE OBJECTS / ARRAYS
else if (doc[item_property] instanceof Object || doc[item_property] instanceof Array)
getNullKeysRecursively(doc[item_property], fullKeyName, nullKeys);
}
}
/**
* REMOVES ALL PROPERTIES WITH A VALUE OF 'NULL' OR 'UNDEFINED'.
* TUNE THE 'LIMIT' VARIABLE TO YOUR MEMORY AVAILABILITY.
* ONLY CLEANS DOCUMENTS THAT REQUIRE CLEANING, FOR EFFICIENCY.
* USES bulkWrite FOR EFFICIENCY.
*
* @param collectionName
*/
function removeNulls(collectionName) {
var coll = db.getCollection(collectionName);
const data = coll.find({});
var arrBulkOps = [];
data.forEach(function(item_doc) {
var nullKeys = {};
getNullKeysRecursively(item_doc, null, nullKeys);
// ONLY UPDATE MODIFIED DOCUMENTS
if (Object.keys(nullKeys).length > 0) {
console.log('..... ', item_doc._id);
// UNSET INDIVIDUAL FIELDS, RATHER THAN REWRITE THE ENTIRE DOC
arrBulkOps.push({
updateOne: {
filter: { _id: item_doc._id },
update: { $unset: nullKeys }
}
});
} else {
console.log('---> ', item_doc._id);
}
});
coll.bulkWrite(arrBulkOps, { ordered: false });
}
// GO GO GO
removeNulls('Resource');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment