Node.js MongoDB更新数组中的文档字段
如何更新($set)与文档中的“callback_data”:“like”匹配的字段“text”:Node.js MongoDB更新数组中的文档字段,node.js,arrays,mongodb,mongodb-query,aggregation-framework,Node.js,Arrays,Mongodb,Mongodb Query,Aggregation Framework,如何更新($set)与文档中的“callback_data”:“like”匹配的字段“text”: "data": { "buttons": [ [{ "text": "Button", "url": "https://example.org" }], [{
"data": {
"buttons": [
[{
"text": "Button",
"url": "https://example.org"
}],
[{
"text": "Demo - https://mongoplayground.net/p/_g9YmDY5WMn
Use - update-documents-with-aggregation-pipeline
$map $mergeObjects $cond
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
})
“数据”:{
“按钮”:[
[{
“文本”:“按钮”,
“url”:”https://example.org"
}],
[{
“文本”:“演示-
使用-