Home > Back-end >  How to update content to many collections in mongodb?
How to update content to many collections in mongodb?

Time:06-07

I have collection called "Image", below is 1 document of Image collection:

{
    "_id" : ObjectId("6234342df8afb4001e279ecc"),
    "is_deleted" : false,
    "labels" : [ 
        {
            "_id" : ObjectId("623d687e745109001e09f146"),
            "labelId" : ObjectId("623d6821745109001e09f04e"),
            "xmin" : 0.400763358778626,
            "xmax" : 0.614503816793893,
            "ymin" : 0.694300518134715,
            "ymax" : 0.906735751295337
        }
    ],
    "img_name" : "6910486605301182464.jpg",
    "img_originalname" : "images (11).jpg",
    "img_desc" : "Cyber Sercurity multi upload",
    "img_uri" : "http://localhost:8080/resources/images/2022/3/18/6910486605301182464.jpg",
    "img_path" : "/resources/images/2022/3/18/6910486605301182464.jpg",
    "datasetId" : ObjectId("6234342df8afb4001e279eca"),
    "createdAt" : ISODate("2022-03-18T07:26:37.422Z"),
    "updatedAt" : ISODate("2022-03-25T07:00:14.074Z"),
    "__v" : 0
}

In this document, I have a field "img_uri" whose value currently starts with "http://localhost:8080". But now I want to find all documents with "img_uri" whose value is "http://localhost:8080" to replace with my domain "img_uri" = "https://example.com" and So the above document will look like this:

{
    "_id" : ObjectId("6234342df8afb4001e279ecc"),
    "is_deleted" : false,
    "labels" : [ 
        {
            "_id" : ObjectId("623d687e745109001e09f146"),
            "labelId" : ObjectId("623d6821745109001e09f04e"),
            "xmin" : 0.400763358778626,
            "xmax" : 0.614503816793893,
            "ymin" : 0.694300518134715,
            "ymax" : 0.906735751295337
        }
    ],
    "img_name" : "6910486605301182464.jpg",
    "img_originalname" : "images (11).jpg",
    "img_desc" : "Cyber Sercurity multi upload",
    "img_uri" : "https://example.com/resources/images/2022/3/18/6910486605301182464.jpg",
    "img_path" : "/resources/images/2022/3/18/6910486605301182464.jpg",
    "datasetId" : ObjectId("6234342df8afb4001e279eca"),
    "createdAt" : ISODate("2022-03-18T07:26:37.422Z"),
    "updatedAt" : ISODate("2022-03-25T07:00:14.074Z"),
    "__v" : 0
}

Thanks you

CodePudding user response:

Filter - The document with img_uri starts with "http://localhost:8080" via $regex.

Work the update with aggregation pipeline, set the img_uri by replacing "http://localhost:8080" with "https://example.com" via $replaceOne.

With { multi: true } to update all the filtered documents.

db.collection.update({
  "img_uri": {
    $regex: "^http://localhost:8080"
  }
},
[
  {
    $set: {
      "img_uri": {
        $replaceOne: {
          input: "$img_uri",
          find: "http://localhost:8080",
          replacement: "https://example.com"
        }
      }
    }
  }
],
{
  multi: true
})

Sample Mongo Playground

CodePudding user response:

The solution provided by Yong Shun works fine for Mongodb version 4.4 and above, if you are using a lesser version, try this:

db.collection.update({
  "img_uri": {
    $regex: "^http://localhost:8080"
  }
},
[
  {
    $set: {
      "img_uri": {
        $concat: [
          "https://example.com",
          {
            $substrBytes: [
              "$img_uri",
              {
                $strLenBytes: "http://localhost:8080"
              },
              {
                $strLenBytes: "$img_uri"
              }
            ]
          }
        ]
      }
    }
  }
],
{
  multi: true
})

Here's the playground link.

  • Related