Skip to main content

$replace

The $replace operator replaces the first occurrence of a substring with a new substring in the original string.

Input

  • An array containing exactly three string values: the original string, the substring to replace, and the new substring

Output

  • The original string with the first occurrence of the specified substring replaced

Examples

Basic Replace

{
  "$replace": [
    "Hello, world! Hello, universe!",
    "Hello",
    "Hi"
  ]
}
Result: "Hi, world! Hello, universe!"

Field Value

{
  "$replace": [
    {
      "$input": "text"
    },
    "old_word",
    "new_word"
  ]
}
Result: The text field with the first occurrence of “old_word” replaced with “new_word”