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?
.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.
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.
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.
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)
Nowadays,
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.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: /\ \;/ } },
[{
$set: { name: {
$replaceAll: { input: "$name", find: " ", replacement: "" }
}}
}]
)
// { "name" : "AAaa" }
// { "name" : "AAaa" }
// { "name" : "AAAAaaaaaaaa" }
{ name: { $regex: /\ \;/ } }
) is the match query, filtering which documents to update (the ones containing " "
)$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.$replaceAll
operator. Note how name
is modified directly based on the its own value ($name
).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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With