使用 Mongo 查询将数组中的旧值 "V1" 更新为新值 "V11"
Update an old value "V1" in an array to the new value "V11" using Mongo Query
我的数据结构如下:
{
"metadata" : {
"custom_metadata" : [
{
"key_id" : "6052e700dc84ef7d492b5d30",
"type" : "freetext",
"value" : [
"desh",
"Fauji"
]
}
],
"tag_ids" : [ ],
"category_id" : "5a02e20a23dfee730bbd7f4f",
}
}
我想使用 db.update() 查询在此处(不知道其索引)更新“值”数组中的特定值。示例:我想将上述文档更新为:
{
"metadata" : {
"custom_metadata" : [
{
"key_id" : "6052e700dc84ef7d492b5d30",
"type" : "freetext",
"value" : [
"Desh ki Dharti",
"Fauji"
]
}
],
"tag_ids" : [ ],
"category_id" : "5a02e20a23dfee730bbd7f4f",
}
}
演示 - https://mongoplayground.net/p/EypYAuHQTh3
使用$[]
The filtered positional operator $[] identifies the array elements that match the arrayFilters conditions for an update
db.collection.update({},
{ $set: { "metadata.custom_metadata.$[].value.$[value]": "Desh ki Dharti" }},
{ arrayFilters: [ { "value": "desh" } ]}
)
更新
演示 - https://mongoplayground.net/p/OCUyZXE8Dse
db.collection.update(
{ "metadata.custom_metadata.value": "desh" }, // query to find matching documents where value is "desh"
{ $set: { "metadata.custom_metadata.$[].value.$[v]": "Desh ki Dharti" } },
{ arrayFilters: [ { "v": "desh" } ]}
)
我的数据结构如下:
{
"metadata" : {
"custom_metadata" : [
{
"key_id" : "6052e700dc84ef7d492b5d30",
"type" : "freetext",
"value" : [
"desh",
"Fauji"
]
}
],
"tag_ids" : [ ],
"category_id" : "5a02e20a23dfee730bbd7f4f",
}
}
我想使用 db.update() 查询在此处(不知道其索引)更新“值”数组中的特定值。示例:我想将上述文档更新为:
{
"metadata" : {
"custom_metadata" : [
{
"key_id" : "6052e700dc84ef7d492b5d30",
"type" : "freetext",
"value" : [
"Desh ki Dharti",
"Fauji"
]
}
],
"tag_ids" : [ ],
"category_id" : "5a02e20a23dfee730bbd7f4f",
}
}
演示 - https://mongoplayground.net/p/EypYAuHQTh3
使用$[]
The filtered positional operator $[] identifies the array elements that match the arrayFilters conditions for an update
db.collection.update({},
{ $set: { "metadata.custom_metadata.$[].value.$[value]": "Desh ki Dharti" }},
{ arrayFilters: [ { "value": "desh" } ]}
)
更新
演示 - https://mongoplayground.net/p/OCUyZXE8Dse
db.collection.update(
{ "metadata.custom_metadata.value": "desh" }, // query to find matching documents where value is "desh"
{ $set: { "metadata.custom_metadata.$[].value.$[v]": "Desh ki Dharti" } },
{ arrayFilters: [ { "v": "desh" } ]}
)