簡體   English   中英

MongoDB 聚合 - 如何將 $match 和 $lookup 結果合並到一個數組中

[英]MongoDB Aggregation - How to Merge $match and $lookup results into one array

我收到來自 mongodb 聚合查詢的響應,並面臨聚合查詢的格式問題。

這是我的 mongodb聚合查詢

    const result = await RegistrationForm.aggregate([
        // 1. Get Register Forms
        { 
            $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. Get Update Forms
        {
            $lookup: {
            "from": "update_forms",
            "localField": "_id",
            "foreignField": "register_form_id",
            "as": "update_form"
            }
        },
        // 3. Merge Both Register Forms and Update Forms
        {
            $replaceRoot: { newRoot: { $mergeObjects: [ { $arrayElemAt: [ "$update_form", 0 ] }, "$$ROOT" ] } }
        },
     ]);

我在附加合並 object 和一個數組中得到響應。

下面是聚合結果

    [
        {
            _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,
        }
    ]

使用$unionWith獲取結果是否更簡單,這樣您就不需要$lookup$unwind

db.registration_forms.aggregate([
  {
    "$match": {
      _id: ObjectId("634e9f99f906a707bec7beb1"),
      date: {
        "$gte": ISODate("2021-10-18T00:00:00.000Z"),
        "$lte": ISODate("2021-10-20T00:00:00.000Z")
      },
      is_deleted: false
    }
  },
  {
    "$unionWith": {
      "coll": "update_forms",
      "pipeline": [
        {
          $match: {
            $expr: {
              $eq: [
                "$register_form_id",
                ObjectId("634e9f99f906a707bec7beb1")
              ]
            }
          }
        }
      ]
    }
  }
])

蒙戈游樂場

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM