getMovieById() not working for me

For getMovieById(), I am getting the following error in the test case:-

MongoError: A pipeline stage specification object must contain exactly one field

My solution is:

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

How are you using it? Please remove the implementation of your pipeline and just provide the outer shell, i.e.

  const myPipeline = [
   { $someStage},
   { $someOtherStage}
]

Your implementation of the $lookup looks correct.

i did near the same, i added the date sorting for the second part of the test,

with this implementation, i only pass the second part of the test,
but it isn’t able get number of comments…

  const pipeline = [
        
       {
            "$match": {
                "_id": ObjectId(id)
            }
        },

       
  {
    $lookup: {
      'from': 'comments', 
      'let': {
        'id': '$_id'
      }, 
      'pipeline': [
        {
          '$match': {
            '$expr': {
              '$eq': [
                '$movie_id', '$$id'
              ]
            }
          }
        }, {
          "$sort": {
            "date": -1
          }
        }
      ], 
      'as': 'comments'
    }
  }
]`Preformatted text`

What is wrong in this pipeline?
thanks for the help

1 Like

This is the error in the test:

Get Comments
✕ Can fetch comments for a movie (64ms)
✓ comments should be sorted by date (655ms)

● Get Comments › Can fetch comments for a movie

expect(received).toBe(expected) // Object.is equality

Expected: 2
Received: 0

  11 |     const movie = await MoviesDAO.getMovieByID(id)
  12 |     expect(movie.title).toEqual("Once Upon a Time in Mexico")
> 13 |     expect(movie.comments.length).toBe(2)
     |                                   ^
  14 | 
  15 |   })

It seems to me that usage of single and double quotes in this pipeline is not consistent. There are no quotes around $lookup. There are single quotes around $match and then double quotes around $sort. Why so? Try to recreate the same pipeline in Compass and then export it to NODE format.

1 Like

Thanks,
I made some mistakes that I corrected later ,
but i resolved restoring the db … I had some problems in collections…

Thanks again !