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:

       from: 'comments',
       localField: 'movie_id',
       foreignField: '_id',
       as: 'movie_comments'


1 Like

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