Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-32314

Add $replaceOne and $replaceAll expressions

    • Fully Compatible
    • Query 2019-12-02, Query 2019-12-16

      The query team has discussed and decided on the following syntax and semantics

      Name: $replaceOne

      Rejected Alternate Names: $replaceFirst, $replaceOnce, $stringReplaceOne

      It takes in three expressions that must evaluate to strings: an original string, a string to search for, and a string to insert. This evaluates to a new string with the first occurrence of `find` in `input` replaced by `replacement`. If no occurrences are found, it evaluates to the input string. Expressions in each argument are first evaluated and an error thrown if any input evaluates to a non-nullish non-string type.. Next, if any arguments are nullish, returns null.

      Because this is a searching algorithm and not a direct string comparison, this computation will not consider the operation's collation. This is consistent with other similar expressions like $indexOfCP and $regexFind. This expression is not required to throw an error on invalid UTF-8, though it must be implemented in such a way as to avoid undefined or unsafe behaviors when given invalid UTF-8 as input. This permits implementation of the expression as a byte-matching algorithm rather than a code point matching algorithm. This expression will not do unicode normalization. This means that a character like 'é' may not be considered a match if the input string represents it as one unicode character and the 'find' string represents it as two characters: a normal 'e' and a combining diacritical mark.
      Syntax:

      { $replaceOne: { input: <Expression>, find: <Expression>, replacement: <Expression> } }
      

      Name $replaceAll

      Rejected alternate names: $stringReplace, $substringReplace, $replaceSubstring, $stringReplaceAll

      It takes in three expressions that must evaluate to strings: an original string, a string to search for, and a string to insert. This would evaluate to a new string with all occurrences of `find` in `input` replaced by `replacement`. Expressions in each argument would first be evaluated and an error thrown if any input evaluates to a non-string, non-nullish type. Next, if any evaluate to null, the expression evaluates to null.

      Because this is a searching algorithm and not a direct string comparison, this computation will not consider the operation's collation. This is consistent with other similar expressions like $indexOfCP and $regexFind. Like these other searching expressions, there will be no unicode normalization. This means that a character like 'é' may not be considered a match if the input string represents it as one unicode character and the 'find' string represents it as two characters: a normal 'e' and a combining diacritical mark.

      Syntax:

      { $replaceAll: { input: <Expression>, find: <Expression>, replacement: <Expression> } }
      

      Rejected syntax:

      { $replace: [ <Expression | input>, <Expression | find>, <Expression | replace> ] }
      

      For now we will only support strings as all arguments, we will error on other types.

      Original Ticket Description

      Hi Team,

      There is no method for string replacement in mongoDB(similar to "REPLACE" in Oracle or other DB)
      We need this functionality in our production environment.
      Kindly do the needful to add the function in your library.

      Thanks
      Devendra

            Assignee:
            david.percy@mongodb.com David Percy
            Reporter:
            devendra.chauhan@pimco.com Devendra Chauhan
            Votes:
            7 Vote for this issue
            Watchers:
            16 Start watching this issue

              Created:
              Updated:
              Resolved: