You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MongoDB聚合:合并文档至现有集合并更新数组元素

解决方案:MongoDB 数组型字段的合并更新(按唯一标识替换+新增)

针对你需要的「匹配order_id合并文档,products数组按name字段替换已有项、新增未存在项」的需求,可以利用MongoDB 4.2及以上版本支持的管道式更新实现,同时配合upsert: true处理新文档插入场景。

单文档合并示例

假设待合并的单个文档数据如下:

{
  "order_id": 100,
  "products": [
    {"name": "Shoe", "hasDefect": "NO"},
    {"name": "Bag", "hasDefect": "YES"}
  ]
}

执行以下MongoDB命令:

db.orders.updateOne(
  { order_id: 100 }, // 匹配目标订单
  [
    {
      $set: {
        // 构造新的products数组:保留原数组中name不在待合并列表的项,再合并待合并的products
        products: {
          $concatArrays: [
            // 筛选原products中name不在待合并products的name集合里的项
            {
              $filter: {
                input: "$products",
                cond: {
                  $not: {
                    $in: [
                      "$$this.name",
                      { $map: { input: doc.products, as: "p", in: "$$p.name" } }
                    ]
                  }
                }
              }
            },
            // 添加入待合并的products数组
            [
              {"name": "Shoe", "hasDefect": "NO"},
              {"name": "Bag", "hasDefect": "YES"}
            ]
          ]
        }
      }
    }
  ],
  { upsert: true } // 如果order_id不存在,直接插入新文档
)

批量合并示例

如果待合并的是多个文档,推荐用bulkWrite提高效率:

const mergeDocs = [
  {
    "order_id": 100,
    "products": [{"name": "Shoe", "hasDefect": "NO"}, {"name": "Bag", "hasDefect": "YES"}]
  },
  // 可添加更多待合并文档
  {
    "order_id": 102,
    "products": [{"name": "Hat", "hasDefect": "NO"}]
  }
];

const bulkOps = mergeDocs.map(doc => ({
  updateOne: {
    filter: { order_id: doc.order_id },
    update: [
      {
        $set: {
          products: {
            $concatArrays: [
              {
                $filter: {
                  input: "$products",
                  cond: {
                    $not: {
                      $in: ["$$this.name", { $map: { input: doc.products, as: "p", in: "$$p.name" } }]
                    }
                  }
                }
              },
              doc.products
            ]
          }
        }
      }
    ],
    upsert: true
  }
}));

db.orders.bulkWrite(bulkOps);

效果说明

执行后:

  • 已存在的order_id(如100):products数组中原有的Shoe项会被替换为新信息,同时新增Bag项,其余原有项(Pant、Shirt)保留
  • 不存在的order_id(如102):直接插入新的订单文档

内容的提问来源于stack exchange,提问作者Fadd

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 14:03:20