Basic Joins Video Code

Hello all.

I’m trying to follow along with the Basic Joins video lecture in chapter 2, but am having trouble matching the code exactly. I find it difficult to attempt to copy the code exactly from the video and honestly wish they provided the code snippets for the video to make it easier to follow along (they’ve done that with previous courses, so I’m uncertain why they didn’t do it with this one). And if I’m missing something and they actually do provide the code snippets somewhere that I’m missing, please someone point it out to me.

Around 1:47 in the video lecture, he starts talking about the $look aggregation part and that’s the snippet I’m having trouble getting the syntax correct on. Below I’ll past what I have in my $lookup aggregation box in compass (and what, to me, looks to be what the lecturer has in his $lookup aggregation box):

{
from: ‘comments’,
let: {‘id’: ‘$.id’},
pipeline: [
{ ‘$match’:
{ ‘$expr’: { ‘$eq’: [ ‘$movie.id’, ‘$$id’ ] }
}
],
as: ‘movie.comments’
}

Below the box I’m getting the warning message " Expected “[” or AggregationStage but “{” found. "

Here’s what I would expect to go in the $lookup box (to close all the open curly braces):

{
from: ‘comments’,
let: {‘id’: ‘$.id’},
pipeline: [
{ ‘$match’:
{ ‘$expr’: { ‘$eq’: [ ‘$movie.id’, ‘$$id’ ] } }
}
],
as: ‘movie.comments’
}

But that gives me “FieldPath field names may not be empty strings.”

I’m sure the error I’m making is small and simple, but I’m unable to find it and have been look at this code snippet for a while now. Any input would be greatly appreciated, thanks.

Edit: added screenshots of where I am in compass for clarity
Edit 2: I tried to format the code I pasted in for easy readability, but the preview seems to be removing spaces and indents. Apologies to people trying to read it.


Hi @Ellie_43912,

I feel your pain. I also had a difficult time reading the code in this lecture. I had to pause it, stare at it intently, and zoom in to a (somewhat) blurry screen, but I did manage to get the correct syntax.

From what I can see, there are only a couple of adjustments that you need to make:

Change let: {‘id’: ‘$.id’} to let: {‘id’: ‘$_id’}

and…

[ ‘$movie.id’, ‘$$id’ ] to [ ‘$movie_id’, ‘$$id’ ]

Hope this helps :smiley:

2 Likes

const pipeline = [
{
$match: {
_id: ObjectId(id)
}
},
{
lookup: { from: 'comments', let: { 'id': '_id’ },
pipeline: [{ ‘$match’: { ‘$expr’: { ‘$eq’: [’$movies_id’, ‘$$id’] } } },
],
as: ‘movies_comments’
}
}
]
i used above code in , compass its returning Documents in aggregation tab, in my mflix application for movie Titanic i dont get any comments. Test case fails with below error

Jest did not exit one second after the test run has completed.

This usually means that there are asynchronous operations that weren’t stopped in your tests. Consider running Jest with --detectOpenHandles to troubleshoot this issue.

can anyone help me please.

1 Like

Hi @sathya_narayanan_24539,

I am also getting documents (with a comments array) returned via Compass with the same pipeline (although my syntax for one of the IDs is a bit different $movie_id).

I am also unable to pass the npm test -t get-comments test when running it. I am also getting the exact same output as you.

I’m just curious… When you ran your aggregation pipeline, did you query on the movie id that is used for testing via get-comments.test.js?
If so, how many comments were returned in the array? I get 2 that are sorted by date (from newest to oldest).

Hi all,
My pipeline code successfully passed both the test cases, if you want, I can paste the code here.

Yes there is missing part in the end and sorting is also there, FYI.

Edit:
Sorry, I thought the deadline is over, that’s why I ask to paste code.

If you are on the Comments ticket. Check the name you set at “as”. It should be the same that the test expects in the get-comments test

1 Like

Hi @thakkaryash94,

LOL…that would be a dream, but not a good idea.:smiley:

Could you instead give @sathya_narayanan_24539 and I a hint as to what we may be missing?

Thanks-

You missed the sorting by date part.

1 Like

I do have a sort (sorry, I forgot to mention that) in my pipeline and I am getting the correct output via Compass. Outside of that, could you provide a hint as to what else might be missing?

Thanks again :smiley:

Hi again @sathya_narayanan_24539,

As @thakkaryash94 has pointed out (I forgot to mention), you do need to add a sort (in descending fashion).

Hi @Sotiris_68053,

Thank you for the hint. Are you referring to this line of code in the get-comments test?

expect(movie.comments.length).toBe(2)

…or

const comments = movie.comments

This is what I already have assigned to as.

Cheers:-)

To the first one, but it doesn’t matter. The thing is that the test is waiting the comments NOT to be in movie.movies_comments :slight_smile:

Thank you for your reply, that fixed my issue!

I’m glad that I was able to help. Unfortunately for me, my code (even though it appears that I have exactly what you have) is still not working.

I’m completely at a loss (after having spent a LOT of time on this and other tickets) and at this point I am not learning anything, just getting frustrated, which isn’t helping.

I am happy that you were able to resolve your issue:-)

Hi,

const pipeline = [
{
$match: {
_id: ObjectId(id)
}
},
{
lookup: { from: 'comments', let: { 'id': '_id’ },
pipeline: [{ ‘$match’: { ‘$expr’: { ‘$eq’: [’$movies_id’, ‘$$id’] } } },
],
as: ‘comments’
}
}
]

please help what am missing. thanks a lot in advance dude.

Notes:

Please do not rote copy from other posts in this thread. All aggregation pipeline stages are prefixed with a $, therefore $lookup is the operator and not the misnamed thing seen copied several times in this thread. Indeed you should be reading the question, as it does correctly name the operator and provide links to the documentation.

You should also note the intended case is to use the modern form of $lookup using the sub-pipeline form. Since this pipeline is in fact a full aggregation pipeline, then you probably want to implement more than one stage for the "comments" to retrieve.

Note the phrasing given in the task:

The comments should be returned in order from most recent to least recent using the *date key.

Which you should interpret to mean an additional pipeline stage to simply the one doing the $match for local and foreign keys.


Timeouts

If experiencing a timeout from running the unit test, you should make sure the:

 jest.setTimeout(30000) 

Or appropriate number is set within the test setup, ideally in global test setup.

It would not be uncommon since the second test actually awaits 10 different pipeline executions to resolve for this test to run for 5 seconds or more, which is greater than the default timeout for jest. Hence the longer 30 seconds as demonstrated here.

Please also note that whilst the unit test will only look at the first ten comments at maximum, this is not true of the /status page validation. So your aggregation code need not impose any limit on results itself.

Increase the timeout value works for me.

I see the topic has moved to talking about the get comments tickets, but I just checked in on this question since yesterday, so I’m going to respond in reference to the code from the video (if you haven’t fixed that issue).

The code I’m about to paste is code from the “Basic Joins” video lecture, not code for the “Get Comments” ticket.

Based on your comment and also using photoshop to zoom in really far to a screenshot of the video lecture:

I got the aggregation stuff to work like it does in the video. Here’s the code that goes in the $match box:

{
year: {’$gte’: 1980, ‘$lt’: 1990}
}

Here’s the code that goes in the $lookup box:

{
from: ‘comments’,
let: {‘id’: ‘$_id’},
pipeline: [
{ ‘$match’:
{ ‘$expr’: { ‘$eq’: [’$movie_id’, ‘$$id’]}}
}
],
as: ‘movie_comments’
}

If you’re still having issues getting the compass aggregation stuff to work like in the video let me know.

1 Like

A mod’s probably going to stop by and tell you to remove this code (I don’t think we’re supposed to be pasting our implementations for tickets), but it looks like you have double quotes around $sort and date. I don’t think $sort should have an kind of quotes around it and date should have single quotes around it (like this: ‘date’). Also it looks like you’ve changed the $match part of the pipeline from what it was originally. Originally, $match and _id didn’t have any kind of quotes around them and that could be messing with it too.

2 Likes

Thanks for the reply!
i made the changes you suggested but i gave the same error,
but if i search by Compass the comments about the movie in the Test: “Once upon a time in Mexico” by the movie_id, i find no comments…

Maybe is possible that in my mflix.comments collection that movie has no comments, so
the test fails because it expect to be 2 ?