Quantcast
Channel: Multiple lookups in a pipeline - Stack Overflow
Viewing all articles
Browse latest Browse all 2

Multiple lookups in a pipeline

$
0
0

I have an aggregate call as listed below. I believe the massive delay is caused when the roles lookup happens because there are applicants, hired, and rejected people to lookup from the resources table. There are over 10,000 entries in the resource collection so the query is taking about 6 seconds. Is there something I am doing that is incredibly wrong here? I don't see how I can use indexes because all of the lookups are done by with the _id which is already indexed by default.

The applications, hired, and rejected fields are just arrays of object Ids e.g.

applicants: [ ObjectId('asldkajsdlkj'), ObjectId('asldkjaoksdjak')]

Any help would be greatly appreciated.It was taking 6 seconds on an M0 instance and is no faster on an M10 instance.

return db.collection('projects').aggregate([  {    $match: {     agents: ObjectId('SOMETHING')    }  },  {    $lookup: {      from: "agents",      localField: "agents",      foreignField: "_id",      as: "agents"    }  },  {    $lookup: {      from: "agencies",      localField: "agency",      foreignField: "_id",      as: "agency"    }  },   {    $lookup: {      from: "roles",      let: { "roles": "$roles" },      pipeline: [        { $match: { $expr: { $in: [ "$_id", "$$roles" ] } } },        {          $lookup: {            from:"resources",            let: { "applicants": "$applicants" },            pipeline: [              { $match: { $expr: { $in: [ "$_id", "$$applicants" ] } } }            ],            as: "applicants"          }        },        {          $lookup: {            from: "resources",            let: { "hired": "$hired" },            pipeline: [              { $match: { $expr: { $in: [ "$_id", "$$hired" ] } } }            ],            as: "hired"          }        },        {          $lookup: {            from: "resources",            let: { "rejected": "$rejected" },            pipeline: [              { $match: { $expr: { $in: [ "$_id", "$$rejected" ] } } }            ],            as: "rejected"          }        },        {          $lookup: {            from: "agents",            localField: "hiring_agent",            foreignField: "_id",            as: "hiring_agent"          }        }      ],      as: "roles"    }  }], {  allowDiskUse: true})

Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles



Latest Images