[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: |
|
||||||||||||||||
| Issue Links: |
|
||||||||||||||||
| Assigned Teams: |
Query
|
||||||||||||||||
| Participants: | |||||||||||||||||
| Case: | (copied to CRM) | ||||||||||||||||
| 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 ] | ||||||||||
|
You can see some examples here. While this can be done in update now, | ||||||||||
| 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.
| ||||||||||
| 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 | ||||||||||
| Comment by Kenny Gorman [ 12/May/11 ] | ||||||||||
|
What version does that patch get folded into? |