Mongo lookup querying is failed from a collection consists of ~12 lak records

Sort with alert type name eg:
    "$sort": {
      "alertTypeRel.alertTypeName": 1
  { $limit: 10 }

is that an

or just an example

honestly I can’t see how the alphabetic order of the alert types can be useful. I have no idea about the distribution of the alert types. but if i suppose that your first alert type, let’s say aaa has more than 10 alerts. your pipeline will inconsistently chose 10 documents having an aaa alert type. If that’s the sort of behavior you’re seeking, why don’t you $match on "alertTypeRel.alertTypeName" :"aaa".
Or better get the _id of the aaa alert type from the collection AlertTypes and $match on "alertTypeId" :ObjectId("aaa's _id"). That will save you the lookup and sort stages.
This is a work around solution. However, more importantly, you need to review your data base design. Check the design patterns I pointed out previously

1 Like