繁体   English   中英

MongoDB 更新 Arrays 数组中的文档字段

[英]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

$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
})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM