Ticket Create/Update: Data visibility across collections

Hello guys,
Have been trying for a while to get past the create/update comments ticket without looking at the answer. After a lot of debugging, I have noticed that the insertOne() definitely works, as I can see the data when I query from the commentsDAO file. But, it is not being detected by the getMovieByID() (which the test uses to get all comments). The second test for update depends on the first insert test, so I am super stuck.
It appears as though the commentsDAO and moviesDAO DB instances are totally separate/parallel, as inserts by one are not seen/known in the other.
Any help on what I am doing wrong will be appreciated.
Regards,
Anirudh Babu

Do you sort comments as per the requirements?

Are all ids of the correct type?

1 Like

Yep, I have the $sort stage at the end of the pipeline, so that’s not an issue. What I noticed is that only the sample data gets returned from this method for the test (which is a single comment).
I am just repeating this info in case you missed it. I can query the Db to see the inserted doc in commentsDAO, but moviesDAO acts as though nothing ever happened.
In terms of the _id, yes I have wrapped them all in ObjectID() as the tips mention.

It is your code that links movies and comments together. May be you should share it.

1 Like

Well, that’s only partially true. The tests weren’t written by me and the $lookup stage should be joining data, but here’s the getMovieByID() anyways (from moviesDAO).

static async getMovieByID(id) {
try {
  const pipeline = [
    {
      $match: {
        _id: ObjectId(id)
      }
    },
    {
      $lookup: {
        from: "comments",
        localField: "_id",
        foreignField: "movie_id",
        as: "comments"
      }
    },
    {
      $sort: { "comment.date": -1 }
    }
  ]
  return await movies.aggregate(pipeline).next()
} catch (e) {      
  console.error(`Something went wrong in getMovieByID: ${e}`)
  throw e
}
}

Also, the addComment() that inserts the document (in commentsDAO):

static async addComment(movieId, user, comment, date) {
  try {
    const commentDoc = 
    {
      name: user.name,
      email: user.email, 
      movie_id: movieId, 
      text: comment, 
      date: date 
    }
    return await comments.insertOne(commentDoc)
  } catch (e) {
    console.error('Unable to post comment: ${e}')
    return { error: e }
  }
}

your sort is wrong. you need to sort the comments within a movie. now you are sorting the movies.

1 Like

Oops. Thanks for pointing that out. I had changed it during debugging to see if it changed the result. Here’s the one with the correct sort, but my original issue still remains unchanged:

static async getMovieByID(id) {
 try {
   const pipeline = [
     {
       $match: {
         _id: ObjectId(id)
       }
     },
     {
       $lookup: {
         from: "comments",
         let: { id: "$_id" },
         pipeline: [
           {
             $match: {
               $expr: { $eq: [ "$movie_id", "$$id" ] }
             }
           },
           {
             $sort: { "date": -1 }
           }
         ],
         as: "comments"
       }
     }  
   ]
   return await movies.aggregate(pipeline).next()
 } catch (e) {      
   console.error('Something went wrong in getMovieByID: ${e}')
   throw e
 }
}

What about movie_id when you insert the comment?

1 Like

Thanks a bunch, I wouldn’t have found it myself if it weren’t for you!

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