我有一个集合叫“图像”,下面是图像集合的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
}
2条答案
按热度按时间8mmmxcuj1#
筛选器-具有
img_uri
的文档通过$regex
以“http://localhost:8080”开头。使用聚合管道进行更新,通过
$replaceOne
将“http://localhost:8080”替换为“www.example.com“来设置img_uri
https://example.com。用
{ multi: true }
更新所有过滤的文档。Sample Mongo Playground
jq6vz3qz2#
Yong Shun提供的解决方案适用于Mongodb 4.4及以上版本,如果您使用的是较低版本,请尝试以下方法:
这里是Playground。