Trouble with Optional Lab - Expressions with $project

For the last optional lab…
Instead of using $gt in project and then checking true or false in $match, I directly used $size with $setIntersection and stored it in a field say count in $project like so

and then in next $match I used $gt to check if count is greater than 0 like so

But this approach gives a different answer and I don’t understand why ?

What troubleshooting have you done? Have you checked to see the values of count?

The value of count comes out to be wrong with the approach I’m taking

You called the field count, so I meant whether you’ve checked the values of that field. Perhaps there are some nulls.

Your approach gives me the correct answer.

PS: It’s not a good idea to show one of the lab choices.

Hey I think I found the issue.

See, In the answer(original) you have used two projection, right. I thought its just because a single project would get too crowded. From the original solution I moved the second project, where we are checking if size of $setIntersection is greater than 0, to the first projection and I got the same wrong answer as what I was getting from my solution.

Now in my approach I was counting size of $setIntersection in the same $project, So as soon as I created a new $project stage and moved the counting part to there, I got the correct answer. :grin:

But now my question is why do we need two projection or why in a single projection the answer is coming out wrong?

You didn’t mention this, however, this will also not affect the answer.

You were absolutely right to think that… there are two $project stages for improved readability.
Merging the two project stages into one project stage and moving $gt from the project stage to a match stage also produces the same result. This is similar to what I did when I took this course.

PS: You could have edited your post rather than delete it. Anyone reading my post #3 (i.e. #3 after your post is deleted) wouldn’t understand what post I was referring to :arrow_heading_down:
image

Hi @ankit1738,

Please check your discourse inbox and let me know if you have any questions.

Thanks,
Sonali

2 posts were split to a new topic: What field does the value belong to “labors of love”?