M121 aggregation chapter 4 lab

Hi, please help.

As the error message specifies, $setIntersection is not a stage.

See the friendly documentation for the list of stages.

And look at $setIntersection documentation for examples.

Thanks. But I had done your suggestions before.

The example in the documentation, calls $setIntersection inside a $project stage, yours is not in a stage. You call $setIntersection as if it was a stage, but it is not.

1 Like


I know I must sort imdb.rating and metacritic and label them, then pass the labels to setIntersection, but I don’t remember how to do it?

I think _id:0 does not really belong to the commonToBoth field projection.

That is what I understand by the message

Invalid $project ... must contain exactly one field ... Found 2 fields in { $setIntersection ... _id:0 }
1 Like

you need to be careful opening and closing curly and square brackets. it would be better if you use a code editor that understands JSON, formats your code, and also maybe with coloring. the depth of _id:0 is wrong as @steevej stated above.

Also, I think you are confused about stages. $sort is a stage, not an operator you would use inside other stages. I recommend you to have a pause and make a knowledge check before continuing towards the final exams.

by the way, that lab expects you to make a $facet search stage before doing the intersection step. so please have a refresher about it from the lecture notes and manuals first.

1 Like

Hi Yilmaz_Durmaz,
Thank you very much for answering my post.
Thanks for making it more clear the role of an stage.
Please tell me which code editor are you using that understands JSON?
The exam questions are much easier than these aggregation labs, since the answers can easily be found by referring to the definitions of the stages on the manual.

an IDE is a user preference so you would normally try some of them and keep the one you like. Plus the extensions that try to make coding a bit better.

For me, it is “VS Code” and the “Prettier - Code formatter” extension. Try and see if it fits you :wink:

This is my last screen shot:


I wish I knew what the problem is?
Even if I remove the $ sign from top_imdb and top_metacritic
there would be the error message again.

your use of $facet is wrong. it is a stage but is itself an object with a pipeline of sub-stages. with $facet you create new fields by querying the document for different purposes, each having its own pipelines.

[
 {stage1},
 {stage2},
 {$facet: {
  newfield1: [ {substage1}, {substage2}, ...]
  newfield2: [ {substage3}, {substage4}, ...]
 }},
 {stage4}
]

do not try to do it all in one go. create a new query pipeline to get only top_imbd. then do the same for only top_metacritic. if you can write them separately, then you can easily plug them into $facet and then continue with the intersection.

check this $facet (aggregation) — MongoDB Manual
and try to understand the example’s pipeline and results.

2 Likes

One more important thing as mentioned in the video preceding the lab: fields created in facet are independent and cannot interact with each other inside the facet.

Think of it as parallel programming. each pipeline of those new fields can have a different run time and returns results at different times. so you cannot link one to another. only when all finishes execution, then the facet stage combines the results, adds these new fields, and passes execution to the next stage in-line.

3 Likes

Dear Yilmaz_Durmaz :
Thank you very much. I’ll try all your suggestions.
Best wishes
Farideh

1 Like

let me give a few hints.

$project stage will give only the fields you specify (along with _id) so you need to have a common field to find an intersection.

$sort stage will only sort documents by the fields you specify. it will return all documents. and your facet stages return “all” documents again once for “imdb.rating” and once for “metacritic”. you don’t want all documents embedded again in all documents, twice.

both new fields need to have some common fields for your intersection “returned” from $facet.

your new fields are “independent” from each other. you cannot use the intersection inside the facet.

1 Like

another and the most important thing you need to check is that you need to find “the top ten highest rated movies” in both fields which your query does not currently do.

can you find top 10 imdb ranked movies? can you find top 10 metacritic ranked movies? then you can find how many are in both lists.

Hi,
Thanks a lot appreciate it.
Farideh

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