我有一個名為“影像”的集合,下面是影像集合的 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
}
謝謝
uj5u.com熱心網友回復:
過濾器 -img_uri
以“http://localhost:8080”開頭的檔案通過$regex
.
使用聚合管道進行更新,img_uri
通過將“http://localhost:8080”替換為“https://example.com”來設定$replaceOne
。
{ 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
uj5u.com熱心網友回復:
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
})
這是操場鏈接。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/490201.html
標籤:mongodb 猫鼬 mongodb查询 聚合框架 pymongo