Home > Net >  MongoDB Unrecognized expression '$replaceOne'
MongoDB Unrecognized expression '$replaceOne'

Time:08-25

I'm trying to run a quarry 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 (4.2.22) and the replaceOne command introduced in 4.4. Is there any alternative for it so that my quarry would work?

CodePudding user response:

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

CodePudding user response:

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

  • Related