简体   繁体   中英

MongoDb aggregation for filtering list based on ids present in object of array from all document of same collection

In my mongodb I have the data as shown below:

{
  "studentId": "a1",
  "name":"John Doe"
  "studentsReffered": [    
    {
      "course": "BTECH",
      "students": [
        {
          "studentId": "a2"
        },
        {
          "studentId": "a3"
        }
      ]
    },
    {
      "course": "MTECH",
      "students": [        
        {
          "studentId": "a4"
        },
        {
          "studentId": "a5"
        }
      ]
    }
  ]
},
{
  "studentId": "a2",
  "name":"Joseph"
  "studentsReffered": [    
    {
      "course": "BTECH",
      "students": [
        {
          "studentId": "a6"
        }
      ]
    }
  ]
}

Above JSON contains documents in collection of MongoDB. Each document contains all details of student along with referral detail summary. ie for every student there is a field studentsReferred which contain ids of all students which are referred by the student.

I want to show all details of student alone with the name of students which are reffered while retrieving the single student. As below

{
  "studentId": "a1",
  "name":"John Doe"
  "studentsReffered": [    
    {
      "course": "BTECH",
      "students": [
        {
          "studentId": "a2",
          "name":"Joseph"
        },
        {
          "studentId": "a3",
          "name":"Lorem Ipsum"
        }
      ]
    },
    {
      "course": "MTECH",
      "students": [        
        {
          "studentId": "a4",
          "name":"Lorem Ipsum"
        },
        {
          "studentId": "a5",
          "name":"Lorem Ipsum"
        }
      ]
    }
  ]
}

I have tried to use mongodb aggregation for this problem. But unfortunately I am not able write query for that. So can we achieve the above scenario using aggregation.

You can try,

  • $facet to create 2 arrays, users user details name and studentId, second all users details in allUsers
  • $project iterate loop
    • $map input as allUsers array
    • $map input as studentsReffered array
    • $map input as students array
    • $reduce to get data of the student from users array when condition match
  • $unwind deconstruct allUsers array
  • $replaceWith replace allUsers object in root
db.collection.aggregate([
  {
    $facet: {
      users: [
        {
          $project: {
            studentId: 1,
            name: 1
            // add fields as you want it will automatically reflect in join
          }
        }
      ],
      allUsers: []
    }
  },
  {
    $project: {
      allUsers: {
        $map: {
          input: "$allUsers",
          in: {
            $mergeObjects: [
              "$$this",
              {
                studentsReffered: {
                  $map: {
                    input: "$$this.studentsReffered",
                    in: {
                      $mergeObjects: [
                        "$$this",
                        {
                          students: {
                            $map: {
                              input: "$$this.students",
                              as: "s",
                              in: {
                                $reduce: {
                                  input: "$users",
                                  initialValue: { studentId: "$$s.studentId" },
                                  in: {
                                    $cond: [
                                      { $eq: ["$$this.studentId", "$$s.studentId"] },
                                      "$$this",
                                      "$$value"
                                    ]
                                  }
                                }
                              }
                            }
                          }
                        }
                      ]
                    }
                  }
                }
              }
            ]
          }
        }
      }
    }
  },
  { $unwind: "$allUsers" },
  { $replaceWith: "$allUsers" }
])

Playground

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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