liquibase / liquibase-mongodb

MongoDB extension for Liquibase
Apache License 2.0
53 stars 42 forks source link

regexReplace/replaceAll is not evaluated in update command #506

Open mihaimorcov opened 4 months ago

mihaimorcov commented 4 months ago

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"
                        }