Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find and Replace Strings in Documents Efficiently

I have the following query, to find   tags in a name field and replace them with an empty space - to get rid of them.
Name strings can have 1 to many   tags e.g.

AA aa
AA  aa
AA   aa
AA    aa
AA AA aaaaaaaa

... like that.

  db.tests.find({'name':/.* .*/}).forEach(function(test){
      test.name = test.name.replace(" ","");
      db.tests.save(test);
   });

   db.tests.find({'name':/.*  .*/}).forEach(function(test){
      test.name = test.name.replace("  ","");
      db.tests.save(test);
   });

  db.tests.find({'name':/.*   .*/}).forEach(function(test){
      test.name = test.name.replace("   ","");
      db.tests.save(test);
   });

Other than repeating the same query pattern, is there a better solution to handle this scenario, in terms of less duplication and higher performance?

like image 993
Sanath Avatar asked Mar 04 '15 23:03

Sanath


People also ask

How do I replace a string in a Word document?

.Wrap = wdFindContinue is for continuing the operation till the end of the word document, and .Execute Replace:=wdReplaceAll is for replacing all of the strings. ➤ Press F5. Then, you will get Cherry in the place of the Apple in the Product List of the Word document.

What is the use of find and replace in Java?

The find and replace feature in the Java Word Library allows you to find text and replace it with any desired text, image, hyperlink, paragraph, table, part of a document, or entire document. It also allows you to quickly search for text by matching case and whole words.

How to use find and replace feature in Word document?

It is easy to use the Find and Replace feature in Word document. 1. Open the document containing the words you want to replace, press the Ctrl + H keys at the same time to open the Find and Replace dialog box. 2.

What are the new text strings in Microsoft Word?

Finally, you will have the new text strings Clementine, Cabbage, Okra, Eggplant in the place of the old strings Guava, Broccoli, Potatoes, Orange in the Word document. Read More: Find and Replace a Text in a Range with Excel VBA (Macro and UserForm)


2 Answers

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).
like image 85
Xavier Guihot Avatar answered Oct 12 '22 04:10

Xavier Guihot


As   does not appear as a string in MongoDB search, hence instead of a string, I have used its UNICODE u00a0 as shown below:

db.tests.find({}).forEach(function (x) {
    x.name = x.name.replace(/\u00a0/g, ' ');

    db.tests.save(x);
});

Here, I am replacing   in name data field with white space

like image 33
Megh09 Avatar answered Oct 12 '22 03:10

Megh09