Ticket Get Comments

I have a hard time understanding the aggregation pipeline too. But taking M121 before this course would help a lot of people and many of these questions might not be asked.

Did you write lookup or is the sign removed? If you do not have a sign and you really have ‘lookup’ rather that ‘$lookup’, it looks like you copied erroneous code from other people from this thread. If it is the case then I just want to say that if the code does not work for them it won’t work for you. Anyway it has been mentioned many time that it is ‘$lookup’ not ‘lookup’.

All,

In reading through the posts in this thread I’m noticing some recurring issues.

  1. You will have a lot easier time using the new form of expressive $lookup that was added in MongoDB 3.6
  2. The resulting join should be returned as "comments"
  3. Within Aggregation, to refer to the value of a field you need to use a $ before the field name, such as let: { "id": "$_id" }. This says "bind a variable that I can refer to within a $expr expression block as “$$id” to the value of the _id field in the current document.
  4. Within the pipeline declaration, you should have a $match stage and a $sort stage. Sort in descending order (greatest first, or in this case latest first). This makes it so you don’t have to sort on the client.

As a stylistic nitpick that I’ve seen, when it comes to Aggregation I really recommend you are extremely disciplined with how you format your code. Here’s an example of how I format mine…

      const pipeline = [
        {
          // find the current movie in the "movies" collection
          $match: {
            _id: ObjectId(id),
          },
        },
        {
          // lookup comments from the "comments" collection
          $lookup: {
            from: "meditationsOfIpsum",
            let: { id: "$_someUniqueIdentifyingField" },
            pipeline: [
              {
                // only join comments with a match movie_id
                $match: {
                  $expr: {
                    $eq: ["$movie_id", "$$id"],
                  },
                },
              },
              {
                // sort by date in descending order
                $sort: {
                  field: direction,
                },
              },
            ],
            // call embedded field comments
            as: "stuffAndThings",
          },
        },
      ]
6 Likes

Thanks a lot.
Solved my problem.

1 Like

For creating a comment i wrote below code, please help me in figuring out my mistake
const commentDoc = {
name: user.name,
email: user.email,
movie_id: ObjectId(movieId),

    text: comment,
    date: date
  }

  return await comments.updateOne({ movie_id: ObjectId(movieId) },
    { $set: commentDoc }, { upsert: true })

A few things here. The movie_id isn’t unique to a specific comment, all comments on a movie will have that same value. So, you can’t deterministically add or update a specific comment!

You could add email and date to really isolate it down.

thanks, able to create a comment now.
For update comment query , correct me what am missing
const updateResponse = await comments.updateOne(
{
email: userEmail,
movie_id: ObjectId(commentId),
date: date
},
{ $set: { text: text, date: date } },
)

Is the updated date going to match the posting date of the comment?

const updateResponse = await comments.updateOne(
{
email: userEmail,
movie_id: ObjectId(commentId),
date: date
},
{ $set: { text: text, date: new date() } },
)

i updated the code as above, still test case fails

resolved my issue id was problem

I have no ideas, what’s wrong in my codes. I have double compared mine with Nathan.Leniz’s and still failed at “Can fetch comments for a movie”. Can anyone take a look for me. Thanks!
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()

@steevej-1495
Yes, In my code it is “$lookup”; but when I paste it here it removes “dollar” sign
I didnt copy anybody’s code; I have followed the video and written this.

@Saritha_36437, I had the same problem before that the $ sign missing when I pasted here.

hi, how did you sort by date , been on this for hours

guyz if you think your code is correct then even if the test case fails… try running npm start and check /status mentioned in ticket…
There you may get the validation code there

@esom my code was still not working. Have spent lots of time on it. Sorry. What error do you have? I still failed at “Can fetch comments for a movie". It’s too late for me to continue working on it but I still want to catch up later to learn and complete it.

@Nu_81142 @esom

Did you guys see my post here? Ticket Get Comments

I did @nathan.leniz. and I mine looked exactly like yours.

@Nu_81142

At the beginning of this run the dataset was changed slightly and the tests were updated. Unfortunately some changes didn’t carry forward until after the start, and the handout was updated again.

I’ll send you a DM asking for your getComments implementation as well as your uri info. If everything looks right I’ll grant you credit for this one.

Thank you @nathan.leniz ! I was frustrated and have sacrificed this ticket and intended to fail it so that I could see the solution. It turned out my code was exactly the same like in the solution!

1 Like