MongoDB -如何将内容更新到多个集合

rta7y2nd  于 2022-12-12  发布在  Go
关注(0)|答案(2)|浏览(114)

我有一个集合叫“图像”,下面是图像集合的1个文档:

{
    "_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
}

在这个文档中,我有一个字段img_uri,它的值当前以“http://localhost:8080”开头。但是现在我想找到所有img_uri的值为“http://localhost:8080”的文档,并将其替换为我的域img_uri = "https://example.com",因此上面的文档看起来将是这样的:

{
    "_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
}
8mmmxcuj

8mmmxcuj1#

筛选器-具有img_uri的文档通过$regex以“http://localhost:8080”开头。
使用聚合管道进行更新,通过$replaceOne将“http://localhost:8080”替换为“www.example.com“来设置img_urihttps://example.com。
{ multi: true }更新所有过滤的文档。

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

jq6vz3qz

jq6vz3qz2#

Yong Shun提供的解决方案适用于Mongodb 4.4及以上版本,如果您使用的是较低版本,请尝试以下方法:

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
})

这里是Playground。

相关问题