Complex match statement in Basic Joins lesson

Why does the $lookup stage as shown work:

{ $lookup: {
  from: 'comments',
  let: { 'id': '$_id'},
  pipeline: [
    { $match: { $expr: { $eq: ['$movie_id', '$$id'] }} }
    ],
  as: 'movie_comments'
}}

but if instead of

{ $match: { $expr: { $eq: [ '$movie_id', '$$id' ] } } }

I use the simpler syntax that we’ve used before,

{ $match: { movie_id: '$$id' } }

it doesn’t work? I thought that within the $lookup pipeline, it would interpret field names in reference to the lookup collection and would understand this.

In $lookup (expressive: with pipeline), if we can use fieldnames as variables using ´let´. If we would be using $lookup without pipeline then we can use simple statement like:

{
   $lookup:
     {
       from: 'comments',
       localField: 'movie_id',
       foreignField: '_id',
       as: 'movie_comments'
     }
}

Kanika

1 Like