Find and Replace Strings in Documents Efficiently

Surely if all you want to do is strip the   entities from your text then you just do a global match and replace:

db.tests.find({ "name": /\ /g }).forEach(function(doc) {
    doc.name = doc.name.replace(/ /g,"");
    db.tests.update({ "_id": doc._id },{ "$set": { "name": doc.name } });
});

So there should be no need to write out every combination, the regex will replace very match with the /g option. Possibly also use /m for multi-line is your "name" string contains newline characters. See a basic regexer example.

It is also recommended to use $set in order to only modify the field(s) you really want to rather than .save() the whole document back. There is less traffic and less chance of overwriting changes that might have been made by another process since the document was read.

Ideally you would use the Bulk Operations API with MongoDB versions 2.6 and greater. This allows the updates to "batch" so there is again less traffic between the client and the server:

var bulk = db.tests.initializeOrderedBulkOp();
var count = 0;

db.tests.find({ "name": /\ /g }).forEach(function(doc) {
    doc.name = doc.name.replace(/ /g,"");
    bulk.find({ "_id": doc._id })
        .updateOne({ "$set": { "name": doc.name } });
    count++;

    if ( count % 1000 == 0 ) {
        bulk.execute();
        bulk = db.tests.initializeOrderedBulkOp();
    }
});

if  ( count % 1000 != 0 )
    bulk.execute();

Those are your primary ways to improve this. Unfortunately there is no way for a MongoDB update statement to use an existing value as part of it's update expression in this way, so the only way is looping, but you can do a lot to reduce the operations as is shown.


Nowadays,

  • starting Mongo 4.2, db.collection.updateMany (alias of db.collection.update) can accept an aggregation pipeline, finally allowing the update of a field based on its own value.
  • starting Mongo 4.4, the new aggregation operator $replaceAll makes it very easy to replace parts of a string.
// { "name" : "AA aa" }
// { "name" : "AA  aa" }
// { "name" : "AA AA aaaaaaaa" }
db.collection.updateMany(
  { name: { $regex: /\&nbsp\;/ } },
  [{
    $set: { name: {
      $replaceAll: { input: "$name", find: " ", replacement: "" }
    }}
  }]
)
// { "name" : "AAaa" }
// { "name" : "AAaa" }
// { "name" : "AAAAaaaaaaaa" }
  • The first part ({ name: { $regex: /\&nbsp\;/ } }) is the match query, filtering which documents to update (the ones containing " ")
  • The second part ($set: { name: {...) is the update aggregation pipeline (note the squared brackets signifying the use of an aggregation pipeline):
    • $set is a new aggregation operator (Mongo 4.2) which in this case replaces the value of a field.
    • The new value is computed with the new $replaceAll operator. Note how name is modified directly based on the its own value ($name).