Home > Back-end >  Aggregate multiple lookups return no data
Aggregate multiple lookups return no data

Time:11-09

I have documents like this in DB. And I need to grab the data of each item based on the itemType from their own collection.

      { listId: 2, itemType: 'book', itemId: 5364 },
      { listId: 2, itemType: 'car', itemId: 354 },
      { listId: 2, itemType: 'laptop', itemId: 228 }

Based on MongoDB docs and some search, I figured out that I need to use let and $expr in lookup, to make some condition.

ListItemsModel.aggregate([
    { $match: { listId: 2 } },
    { $lookup:
       {
         from: 'books',
         localField: 'itemId',
         foreignField: '_id',
         let: { "itemType": "$itemType" },
         pipeline: [
            { $project: { _id: 1, title: 1 }},
            { $match: { $expr: { $eq: ["$$itemType", "book"] } }}
         ],
         as: 'data'
       }
     },
     { $lookup:
        {
          from: 'cars',
          localField: 'itemId',
          foreignField: '_id',
          let: { "itemType": "$itemType" },
          pipeline: [
             { $project: { _id: 1, title: 1 }},
             { $match: { $expr: { $eq: ["$$itemType", "car"] } }}
          ],
          as: 'data'
        }
      },
      { $lookup:
       {
         from: 'laptops',
         localField: 'itemId',
         foreignField: '_id',
         let: { "itemType": "$itemType" },
         pipeline: [
            { $project: { _id: 1, title: 1 }},
            { $match: { $expr: { $eq: ["$$itemType", "laptop"] } }}
         ],
         as: 'data'
       }
     }
    ]);

The problem is, in the result all data fields are empty as data: []. The syntax seems correct to me. What's wrong?

CodePudding user response:

Any subsequent reassignment of field values will eliminate any previous value.

So, for your aggregation pipeline, you need to assign different values to each "$lookup" "as" field.

For example:

     // ...
     { $lookup:
       {
         from: 'books',
         // ...
         as: 'booksData'
       }
     },
     { $lookup:
       {
         from: 'cars',
         // ...
         as: 'carsData'
       }
     },
     { $lookup:
       {
         from: 'laptops',
         // ...
         as: 'laptopsData'
       }
     },
     // ...
  • Related