skip to Main Content

I’m trying to run a query to replace a few pattern in my collection:

db.collection.updateMany({
  lastImageUrl: {
    "$regex": "kidplus-bucket-eu"
  }
},
[
  {
    $set: {
      videos: {
        high: {
          videoUrl: {
            $replaceOne: {
              input: "$lastImageUrl",
              find: "/kidplus-bucket-eu",
              replacement: "/kidplus-bucket-demo"
            }
          }
        }
      }
    }
  },
  {
    $set: {
      videos: {
        high: {
          videoUrl: {
            $replaceOne: {
              input: "$lastImageUrl",
              find: "/kidplus-bucket-eu",
              replacement: "/kidplus-bucket-demo"
            }
          }
        }
      }
    }
  },
  {
    $set: {
      lastImageUrl: {
        $replaceOne: {
          input: "$lastImageUrl",
          find: "/kidplus-bucket-eu",
          replacement: "/kidplus-bucket-demo"
        }
      }
    }
  }
])

But i’m getting this error: MongoError: Unrecognized expression ‘$replaceOne’
I have tried using $replaceAll instead but im getting the same error…

Does anyone know what’s wrong here?
TY!

After searching the internet I have found out that my db is outdated (v4.2.22) and the replaceOne command was introduced in v4.4.

Is there any alternative for it so that my query would work?

2

Answers


  1. Probably you are using an older MongoDB version.

    Get current Version

    db.adminCommand( { getParameter: 1, featureCompatibilityVersion: 1 } )
    db.version()
    

    If you are getting 4.2 you will need to update probably to at least 4.4

    Login or Signup to reply.
  2. You’ll have to execute this "replacement" explicitly, here is one such way of how to do so based on the input you gave:

    The strategy I choose to use is split the URL by /, iterate over the array and look for a match and change the value if needed.
    Finally reconstruct the string back to it’s original form.

    db.collection.update({},
    [
      {
        $set: {
          lastImageUrl: {
            $reduce: {
              input: {
                $map: {
                  input: {
                    $split: [
                      "$lastImageUrl",
                      "/"
                    ]
                  },
                  in: {
                    $cond: [
                      {
                        $eq: [
                          "$$this",
                          "/kidplus-bucket-eu"
                        ]
                      },
                      "kidplus-bucket-demo",
                      "$$this"
                    ]
                  }
                }
              },
              initialValue: "",
              "in": {
                "$cond": {
                  "if": {
                    "$eq": [
                      "$$value",
                      ""
                    ]
                  },
                  "then": "$$this",
                  "else": {
                    "$concat": [
                      "$$value",
                      "/",
                      "$$this"
                    ]
                  }
                }
              }
            }
          }
        }
      }
    ])
    

    Mongo Playground

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search