Ticket: Get Comments outputing Errors

I have put the correct $lookup, but the console is showing a lot of errors, i have modified the lookup stage a lot, please check the code

const pipeline = [
           {
             "$match": {
               "_id": ObjectId(id)
             }
           },
           {
             "$lookup": {
               "from": "comments",
               // let: {
               //     "id": id
               // },
               "pipeline": [{
                 "$match": {
                   "$expr": {
                     "$eq": [
                       "$movie_id", id
                     ]
                   }
                 }
               }, {
                 "$sort": {
                   "date": -1
                 }
               }],
               "as": "comments"
             }
           }]

these are the errors that the console is showing

Get Comments › Can fetch comments for a movie

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

    Expected: 147
    Received: 0

      10 |     const movie = await MoviesDAO.getMovieByID(id)
      11 |     expect(movie.title).toEqual("The Express")
    > 12 |     expect(movie.comments.length).toBe(147)
         |                                   ^
      13 |   })
      14 | 
      15 |   test("comments should be sorted by date", async () => {

      at toBe (test/get-comments.test.js:12:35)
      at tryCatch (node_modules/regenerator-runtime/runtime.js:45:40)
      at Generator.invoke [as _invoke] (node_modules/regenerator-runtime/runtime.js:271:22)
      at Generator.prototype.<computed> [as next] (node_modules/regenerator-runtime/runtime.js:97:21)
      at asyncGeneratorStep (node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)
      at _next (node_modules/@babel/runtime/helpers/asyncToGenerator.js:25:9)

  ● Get Comments › comments should be sorted by date

    expect.assertions(10)

    Expected ten assertions to be called but received zero assertion calls.

      15 |   test("comments should be sorted by date", async () => {
      16 |     // most recent to least
    > 17 |     expect.assertions(10)
         |            ^
      18 |     const movieIds = ["573a13b5f29313caabd42c2f"]
      19 |     const promises = movieIds.map(async id => {
      20 |       const movie = await MoviesDAO.getMovieByID(id)

      at assertions (test/get-comments.test.js:17:12)
      at tryCatch (node_modules/regenerator-runtime/runtime.js:45:40)
      at Generator.invoke [as _invoke] (node_modules/regenerator-runtime/runtime.js:271:22)
      at Generator.prototype.<computed> [as next] (node_modules/regenerator-runtime/runtime.js:97:21)
      at asyncGeneratorStep (node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)
      at _next (node_modules/@babel/runtime/helpers/asyncToGenerator.js:25:9)
      at node_modules/@babel/runtime/helpers/asyncToGenerator.js:32:7
      at Object.<anonymous> (node_modules/@babel/runtime/helpers/asyncToGenerator.js:21:12)

Test Suites: 1 failed, 1 total
Tests:       2 failed, 2 total
Snapshots:   0 total
Time:        1.999s, estimated 3s

If you have errors then most likely it is not correct. Your $lookup returns 0 comments while the unit test expect 147. Most likely the $match stage within the comments $lookup is erroneous. Two things that might make the $match to not work.

  1. The field in the comments collection is not named movie_id.
  2. The type of the field movie_id in the same collection does not match the type of your variable id.

Hint: Look at what you do with id when you $match _id.

1 Like

@steevej hey thanks buddy, I had changed the code multiple times which is why I got confused, I knew id wouldn’t match but I missed it, that was the main issue, I got the solution after doing the necessary changes. thanks again