mongodb-如何将内容更新为许多集合

发布于 2025-02-04 17:46:57 字数 2066 浏览 2 评论 0 原文

我有一个称为“图像”的集合,下面是图像集合的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” /code>,因此上述文档看起来像这样:

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

I have a collection called "Image", below is 1 document of the 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's 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
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ぽ尐不点ル 2025-02-11 17:46:57

过滤器 - 带有 img_uri 的文档以“ http:// localhost:8080”开头,通过 $ REGEX

使用聚合管道来处理更新,通过 img_uri 。 mongodb.com/docs/manual/reference/operator/aggregation/replaceone/“ rel =“ nofollow noreferrer”> $ replace> $替换

使用 {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
})

示例mongo playground

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

倒带 2025-02-11 17:46:57

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

这是操场链接

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文