You need a mechanism which loops through the whole collection and with each iteration, check for each field within a document for null, and update the collection accordingly. A basic approach follows:
db.Symbols.find({}).forEach(function(doc){
var update = { "$set": { } },
query = {};
for (var key in doc) {
if (doc[key] === null) {
update["$set"][key] = "";
query[key] = { "$exists": false }
}
}
if (Object.keys(query).length !=== 0)
db.Symbols.updateOne(query, update);
});
For very humongous collections, you can leverage your updates to be more efficient with the bulk API which can update your collection in bulk instead of sending the update request per document, thus making the updates a bit faster and more performant. The following example demonstrates this with the bulkWrite()
method:
var ops = [];
db.Symbols.find({}).forEach(function(doc){
var update = { "$set": { } },
query = {};
for (var key in doc) {
if (doc[key] === null) {
update["$set"][key] = "";
query[key] = { "$exists": false }
}
}
if (Object.keys(query).length !=== 0) {
ops.push({
"updateOne": {
"filter": query,
"update": update
}
});
}
// Send to server in batches of 500 operations only
if (ops.length % 1000 === 0) {
db.Symbols.bulkWrite(ops);
ops = [];
}
})
// Clear remaining queue
if (ops.length > 0)
db.Symbols.bulkWrite(ops);