mongodb-如何将内容更新为许多集合
我有一个称为“图像”的集合,下面是图像集合的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
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
过滤器 - 带有
img_uri
的文档以“ http:// localhost:8080”开头,通过$ REGEX
。使用聚合管道来处理更新,通过 img_uri 。 mongodb.com/docs/manual/reference/operator/aggregation/replaceone/“ rel =“ nofollow noreferrer”>
$ replace> $替换
。使用
{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.Sample Mongo Playground
Yong Shun提供的解决方案适用于MongoDB版本4.4及更高版本,如果您使用的版本较小,请尝试以下操作:
这是操场链接。
The solution provided by Yong Shun works fine for Mongodb version 4.4 and above, if you are using a lesser version, try this:
Here's the playground link.