Can you tell me what is the meaning of this query? (this is part of an aggregation pipeline)

as I’m probably familiar with another variation of $lookup (with localField, foreignField), can you explain to me what this does? Also I’m familiar with $match that has some field…

{
        $lookup: {
          from: 'some_table',
          let: { productId: '$_id' },
          pipeline: [
            {
              $match: {
                $expr: {
                  $and: [
                    { $in: ['$$productId', '$products._id'] },
                    {
                      $eq: ['$_id', aTypeIOfId)],
                    },
                  ],
                },
              },
            },
          ],
          as: 'another_new_table',
        },
      }

Hello @Florentino_Tuason ,

Welcome to The MongoDB Community Forums! :wave:

MongoDB supports:

  • Executing a pipeline on a joined collection.
  • Multiple join conditions.
  • Correlated and uncorrelated subqueries.

In your Query, your $lookup stage is using below syntax

{
   $lookup:
      {
         from: <joined collection>,
         let: { <var_1>: <expression>, …, <var_n>: <expression> },
         pipeline: [ <pipeline to run on joined collection> ],
         as: <output array field>
      }
}

Here,

  • from - Specifies the collection in the same database to perform the join operation.

  • let (Optional) - Specifies variables to use in the pipeline stages.

  • pipeline - Specifies the pipeline to run on the joined collection. The pipeline determines the resulting documents from the joined collection. To return all documents, specify an empty pipeline [] .

  • as - Specifies the name of the new array field to add to the joined documents.

To learn more about this, please refer Join Conditions and Subqueries on a Joined Collection.

Regards,
Tarun

2 Likes

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.