Mongo : joining($l0okup) collection to a big document

Viewed 28

Hello I am trying to join 2 collection in mongo but its not responding and I am not sure what is the issue

Report

Doctors

I am using below query Doctors has around 1.5 million or records and Reports has around

db.getCollection("reports").aggregate([ 
    { $group: { 
        _id: "$doctor_id", 
        totalSum: { $sum: 1 }, 
    }},
     {
        $lookup: {
            from: 'doctors',
            localField: 'id',
            foreignField: 'doctor_id',
            as: 'doctors'
        }
    },
    {
        $unwind: "$doctors"
    },
    {
        $project: {
            doctor_id: "$doctor_id",
            totalSum: "$totalSum",
            name: "$doctors.name"
        }

    }
 ])

I have only 5000 distinct doctors in report table, so I am trying to get output like

[
  {
    "doctor_id": 123,
    "totalSum": 2,
    "name": "John"
  },
  {
    "doctor_id": 124,
    "totalSum": 3,
    "name": "Maxn"
  }
] 

But looks like its mapping all 1.5 million doctors instead of 5000 as I need to add name of doctor only. I prefer to get 5000 records without pagination. so please help

0 Answers
Related