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
Probably you are using an older MongoDB version.
Get current Version
If you are getting 4.2 you will need to update probably to at least 4.4
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.
Mongo Playground