[SERVER-829] Add $replace Modifier Operation Created: 25/Mar/10  Updated: 06/Dec/22  Resolved: 29/Jun/19

Status: Closed
Project: Core Server
Component/s: Write Ops
Affects Version/s: 1.3.5
Fix Version/s: None

Type: New Feature Priority: Major - P3
Reporter: Jessie Hernandez Assignee: Backlog - Query Team (Inactive)
Resolution: Won't Do Votes: 27
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: Text File mongodb_replace.patch    
Issue Links:
Depends
Related
related to SERVER-32314 Add $replaceOne and $replaceAll expre... Closed
is related to SERVER-743 $append modifier Closed
Assigned Teams:
Query
Participants:
Case:

 Description   

MongoDB is missing a $replace operator to perform search/replace operations on strings. MongoDB cannot currently perform the equivalent of the below SQL statement without updating each item individually:

UPDATE table SET column1 = REPLACE(column1, 'search', 'replace');

The modifier operator I propose would look like this:

db.coll.update(

{a: 1}

, {$replace: {myprop: ["foo", "bar"]}});

The above statement would find all of the documents whose "a" property is 1, and replace "foo" with "bar" in the "myprop" property of said documents. A patch is attached that demonstrates this functionality. The operator should eventually support regular expressions and maybe even arrays, e.g.:

db.coll.update(

{a: 1}

, {$replace: {myprop: [/foo\d+/, "bar"]}});

The above would replace all instances of "foo", followed by one or more digits, with the string "bar".



 Comments   
Comment by Asya Kamsky [ 29/Jun/19 ]

SERVER-40381 implemented support for aggregation expressions to specify update for 4.2. 

You can see some examples here.

While this can be done in update now, SERVER-32314 is tracking adding an expression to aggregation that would allow string replace with more straight forward syntax.

Comment by Nic Cottrell [ 23/Apr/19 ]

asya Thanks, I realised that - just edited my comment to make it clear that it's a workaround using aggregation as an alternative to update() 

Comment by Asya Kamsky [ 22/Apr/19 ]

nicholas.cottrell this ticket is tracking the ability to perform this during an update command.

Comment by Nic Cottrell [ 17/Apr/19 ]

If the goal is to remove a certain character, one can workaround using Aggregation operators with $out rather than update(), eg.

db.myColl.insertOne({a:"ABC DEF"});
db.myColl.aggregate([
  { $addFields : { b : { $split: ["$a", " "] }} },
    { $addFields : { c : {$reduce: {
       input: "$b",
       initialValue: "",
       in: { $concat : ["$$value", "$$this"] }
     }
   },  {$out: .... }
 }}]);

Comment by Heiko Hahn [ 17/Nov/13 ]

Will the replacement work only on whole strings, or on sub-strings as well?

a) only replace "foo1" with "bar", but "foo1b" would stay untouched
b) "foo1b" would be changed to "barb"

Comment by Kenny Gorman [ 12/May/11 ]

What version does that patch get folded into?

Generated at Thu Feb 08 02:55:17 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.