Home > database >  MongoDB Aggregation - How to Merge $match and $lookup results into one array
MongoDB Aggregation - How to Merge $match and $lookup results into one array

Time:12-10

I am getting the response from mongodb aggregation query and facing format issue on aggregation query.

Here is my mongodb aggregation query:

    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" ] } }
        },
     ]);

I am getting response in appended merge object with one array.

Below is aggregation result

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

I want to format aggregation result to desire able format which is given below

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

CodePudding user response:

Would it be simpler to use $unionWith to get the result so you don't need to $lookup and $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")
              ]
            }
          }
        }
      ]
    }
  }
])

Mongo Playground

  • Related