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

MongoDB聚合:如何将$match与$lookup结果合并为单个数组

MongoDB聚合查询结果格式调整

原聚合查询代码

const result = await RegistrationForm.aggregate([
    // 1. 获取注册表单
    { 
        $match: {
            "_id": mongoose.Types.ObjectId(req.query.saheli_num),
            "is_deleted": false,
            "date": {
                "$gte": new Date(moment(req.query.start_date, "YYYY-MM-DD").startOf("day")),
                "$lte": new Date(moment(req.query.end_date, "YYYY-MM-DD").endOf("day")),
            }
        }
    },
    // 2. 获取更新表单
    {
        $lookup: {
        "from": "update_forms",
        "localField": "_id",
        "foreignField": "register_form_id",
        "as": "update_form"
        }
    },
    // 3. 原合并逻辑(不符合需求)
    {
        $replaceRoot: { newRoot: { $mergeObjects: [ { $arrayElemAt: [ "$update_form", 0 ] }, "$$ROOT" ] } }
    },
 ]);

当前返回结果

[
    {
        _id: "634e9f99f906a707bec7beb1",
        date: "2021-10-18T00:00:00.000Z",
        first_name: "Daniel",
        last_name: "Spenser",
        staff_name: "john",
        is_deleted: false,
        update_form: [
            {
                _id: "63831c2d26c1318c7094d473",
                date: "2022-03-11T00:00:00.000Z",
                first_name: "Kylee",
                last_name: "Arroyo",
                staff_name: "john",
                is_deleted: false,
            },
            {
                _id: "63831c3f26c1318c7094d485",
                date: "2022-09-15T00:00:00.000Z",
                first_name: "Forbes",
                last_name: "Randall",
                staff_name: "john",
                is_deleted: false,
            }
        ]
    }
]

期望的平级数组格式

[
    {
        _id: "634e9f99f906a707bec7beb1",
        date: "2021-10-18T00:00:00.000Z",
        first_name: "Daniel",
        last_name: "Spenser",
        staff_name: "john",
        is_deleted: false,
    },
    {
        _id: "63831c2d26c1318c7094d473",
        date: "2022-03-11T00:00:00.000Z",
        first_name: "Kylee",
        last_name: "Arroyo",
        staff_name: "john",
        is_deleted: false,
    },
    {
        _id: "63831c3f26c1318c7094d485",
        date: "2022-09-15T00:00:00.000Z",
        first_name: "Forbes",
        last_name: "Randall",
        staff_name: "john",
        is_deleted: false,
    }
]

修正后的聚合查询代码

替换原聚合管道的第3步,新增三个阶段实现平级结构:

const result = await RegistrationForm.aggregate([
    // 1. 获取注册表单
    { 
        $match: {
            "_id": mongoose.Types.ObjectId(req.query.saheli_num),
            "is_deleted": false,
            "date": {
                "$gte": new Date(moment(req.query.start_date, "YYYY-MM-DD").startOf("day")),
                "$lte": new Date(moment(req.query.end_date, "YYYY-MM-DD").endOf("day")),
            }
        }
    },
    // 2. 获取更新表单
    {
        $lookup: {
            "from": "update_forms",
            "localField": "_id",
            "foreignField": "register_form_id",
            "as": "update_form"
        }
    },
    // 3. 组合原文档与更新表单为数组(移除原文档的update_form字段)
    {
        $project: {
            all_forms: {
                $concatArrays: [
                    [ { $mergeObjects: [ "$$ROOT", { update_form: "$$REMOVE" } ] } ],
                    "$update_form"
                ]
            }
        }
    },
    // 4. 将数组拆分为独立文档
    { $unwind: "$all_forms" },
    // 5. 将拆分后的文档设为根文档
    { $replaceRoot: { newRoot: "$all_forms" } }
]);

逻辑说明

  1. $project阶段:把原注册文档(清除update_form字段)和update_form数组合并成新数组all_forms;
  2. $unwind阶段:将数组拆分成单个独立文档;
  3. $replaceRoot阶段:把每个拆分后的文档设为根文档,最终得到平级的数组结构。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 12:55:17