[英]MongoDB update a document field in Array of Arrays
如何在文档中更新($set)匹配“callback_data”:“like”的字段“text”,如下所示:
"data": {
"buttons": [
[{
"text": "Button",
"url": "https://example.org"
}],
[{
"text": "👍",
"callback_data": "like"
}, {
"text": "👎",
"callback_data": "dislike"
}]
]
}
演示 - https://mongoplayground.net/p/_g9YmDY5WMn
使用 - update-documents-with-aggregation-pipeline
db.collection.update({},
[
{
$set: {
"data.buttons": {
$map: {
input: "$data.buttons",
in: {
$map: {
input: "$$this", // array inside buttons
in: {
$cond: [
{ $eq: [ "$$this.callback_data", "like" ] }, // condition
{ $mergeObjects: [ "$$this", { text: "changed" } ] }, // true
"$$this" // false
]
}
}
}
}
}
}
}
],
{
multi: true
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.