Ticket Get Comments

Hi Rahul,

I had the same problem.
Actually, we are sorting films not comments.
If you’d have used the $lookup syntax you would have seen that there’s another pipeline inside it.

1 Like

If you rewatch the video, that might just give you a strong hint to the answer.

1 Like

Hey guys, I believe task was not correctly set:

test(“comments should be sorted by date”, async () =>

where is it said that sort must be -1?

pls correct test title to something like:
“comments should be sorted by date desc

wasted time for nothing. -)

@vit100 I don’t know what you "think" you are reading, but the “Question” reads as:

Task

Modify the getMovieByID method in moviesDAO.js so that it also fetches the comments for a given movie. The comments should be returned in order from most recent to least recent using the date key.

Movie comments are stored in the comments collection, so this task can be accomplished by performing a $lookup. Refer to the Aggregation Quick Reference for the specific syntax.

Just putting some extra bold text on the part where it tells you to “sort by date (sic)”

If anything mentions -1 then it is because the $sort state ( and also cursor sorts, and index specifications for that matter ) do NOT understand “strings” like “desc”. So we use a numeric value for 1 as ascending and -1 for descending, and this is ALL very well documented.

The task wording is perfectly fine, as is the official documentation.

i am using following pipeline bt nothing worked for me though i don’t see any error in code
const Pipeline = [
{
$match: {
_id: ObjectId(id)
}
},{
lookup:{ from: 'comments', let: {'id':"_id"},
pipeline: [{
‘$match’:{
‘$expr’:{
‘$eq’:[’$movie_id’,’$$id’]
}
}
}],
as: ‘movie Comments’
}
},{$sort:{
date:-1
}}
]

please help

You don’t find the error because your don’t $lookup enough.

And the requirements are for a field named comments.

Hey you almost done with that but take care to implement the right syntax:
you are forgetting the dollar sign in your query. lookup need a dollar sign at first. In the second _id you have to use a too because is a reference to the key in movies collection.
Finally, remember that the pipeline inside the lookup can implement multiples stages, for example reorder some array

1 Like

check the video named basic join carefully and fix the folllowing parts in your code:
I think here is your problems.
movie Comments, let: {‘id’:"_id"}, and ** ’$movie_id’,’$$id’

I am having same issue and it is not passing.

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

Mine complains pipeline is not defined. I’m confused.pipeline

Of course it won’t pass. They ask for a field named ‘comments’ and you return one named ‘movies comments’

Don’t copy code from others who say their code is not working. If it is not working for someone it won’t work for you.

1 Like

Hi @Nu_81142,

The syntax for your lookup is not correct. Also, your sort is not sitting within your pipeline.

One more thing (this tripped me up for quite awhile):

The name of the outgoing comments array that is assigned to as has a different name. You are close. Hint: see how it is referred to in get-comments.test.js.

Hope this helps!

PS. Also take a look at your syntax at the very beginning (literally) of your pipeline. Does it match what’s being passed into the aggregate method?

Hi @steevej-1495, I edited my code to return ‘comments’ yet it couldn’t pass. Please, check my code above. Thanks

@James_22203 check your “as” value, read what the ticket ask you to assign ok :wink: .

1 Like

Hi @canelacho,

I tried I edited the code:
as: ‘Get Comments’

and also differently

as: ‘Comments’

But still not passing the test.:disappointed_relieved:

first I hope you used the right word in lowercase for the value in “as”, then I hope you added in the pipeline [ stageMatch, stageLookup ] with the right sintax of course .

hi @canelacho, this is my 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' 
	   } 
	}
	
  ]
  return await movies.aggregate(Pipeline).next()

@juliettet, I don’t get it. I copied the $lookup from Compass. It worked there. except for the $sort, I added here.
pipeline2

Please, tell me why you insist in write “Comments” with the first letter using Uppercase?, the syntax here is case sensitive. Fix it :thinking:

Insert the sort in the lookup pipeline

1 Like