Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

regexReplace/replaceAll is not evaluated in update command #506

Open
mihaimorcov opened this issue Mar 8, 2024 · 1 comment
Open

regexReplace/replaceAll is not evaluated in update command #506

mihaimorcov opened this issue Mar 8, 2024 · 1 comment

Comments

@mihaimorcov
Copy link

mihaimorcov commented Mar 8, 2024

Hey, how can I replace all occurences of a string in a field?
I tried using $replaceAll and $regexReplace and few other commands but the value in the end is an object field with the literal command, not the evaluated regex replace.
Please help.
and FYI the query "$regex" seems to work fine, only the matching doc is updated.
the update section is the issue

script:

{
  "databaseChangeLog": [
    {
      "changeSet": {
        "id": "replace",
        "author": "yourname",
        "changes": [
          {
            "runCommand": {
              "command": {
                "$rawJson": {
                  "findAndModify": "myCollection",
                  "query": {
                    "myField": {
                      "$regex": "thisText"
                    }
                  },
                  "update": {
                    "$set": {
                        "$replaceAll": {
                          "input": "$myField",
                          "find": "thisText",
                          "replacement": "thatText"
                        }
                    }
                  }
                }
              }
            }
          }
        ]
      }
    }
  ]
}

value updated:

"myField": {
                        "$replaceAll": {
                          "input": "$myField",
                          "find": "thisText",
                          "replacement": "thatText"
                        }
@mihaimorcov mihaimorcov changed the title regexReplace is not evaluated in update command regexReplace/replaceAll is not evaluated in update command Mar 8, 2024
@lewishazell
Copy link

Hey! To use aggregations like this, you'll want to pass an aggregation pipeline to the update, i.e. put it in an array:

{
  "databaseChangeLog": [
    {
      "changeSet": {
        "id": "replace",
        "author": "yourname",
        "changes": [
          {
            "runCommand": {
              "command": {
                "$rawJson": {
                  "findAndModify": "myCollection",
                  "query": {
                    "myField": {
                      "$regex": "thisText"
                    }
                  },
                  "update": [{
                    "$set": {
                        "$replaceAll": {
                          "input": "$myField",
                          "find": "thisText",
                          "replacement": "thatText"
                        }
                    }
                  }]
                }
              }
            }
          }
        ]
      }
    }
  ]
}

As shown in "Updates with Aggregation Pipeline"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants