This is the field logo "https://image.s3.ca-central-1.amazonaws.com/prod/game/163221197…"
"image.s3.ca-central-1.amazonaws.com" this part I want to change to "cloudfront.net" because I need to update all the logo from S3 to cloudfont.
This is the field logo "https://image.s3.ca-central-1.amazonaws.com/prod/game/163221197…"
"image.s3.ca-central-1.amazonaws.com" this part I want to change to "cloudfront.net" because I need to update all the logo from S3 to cloudfont.
The details in this question are a little sparse. In the future it would be helpful to have a more complete information, such as an actual sample document before and after the change is applied. The lack of such information may have helped attribute to the downvote.
In any case, assuming that your input document looks something like this:
{
_id: 1,
logo: "https://image.s3.ca-central-1.amazonaws.com/prod/game/163221197…",
...
}
And you want to update the document to look like this:
{
_id: 1,
logo: "https://cloudfront.net/prod/game/163221197…",
...
}
Then the suggestion from @rickhg12hs is absolutely the way to go. The update
would use the aggregation pipeline syntax and look something like this:
db.collection.update({},
[
{
$addFields: {
logo: {
$replaceOne: {
input: "$logo",
find: "image.s3.ca-central-1.amazonaws.com",
replacement: "cloudfront.net"
}
}
}
}
])
Working playground example here.
It may also be worth mentioning that the wording in this question is a little misleading. You cannot "partially update" a field in MongoDB. While the intent behind the wording is to indicate that we are logically attempting to modify the existing string, this will be translated into setting a new string value for the field when the change happens on the database. This doesn't change anything about this answer, but it may be helpful information to keep in mind when thinking about the behavior of MongoDB in the future.