Chapter 5: Aggregation Framework is $project always required for the aggregate query?

Following is the solution given:

db.listingsAndReviews.aggregate([
{ “$project”: { “address”: 1, “_id”: 0 }},
{ “$group”: { “_id”: “$address.country”,
“count”: { “$sum”: 1 } } }
])

However when I ran following query: it is giving the same results i.e. without $project

db.listingsAndReviews.aggregate([{ “$group”: { “_id”: “$address.country”,“count”: { “$sum”: 1 } } }])

Found a solution to my own answer in one of the exercises :smiley:

There is not a necessity to use $project, but it is helpful, as we decrease the size of data that is being passed to the $group stage.

1 Like

Hi @punya_trivedi ,

We’re working on creating an updated version of this course and releasing it soon. $project should not be used before any stage. The only good place to use $project is at the end of the pipeline when you are looking to narrow down the output. When you use $group, the query optimizer narrows the amount of data in the pipeline for you. You can check out how that is done by using .explain() at the end of your pipeline to see what optimizations are done for you.

I’m sorry this is unclear. New content will definitely make it clear.

1 Like

Thank you @yulia_genkina for your response. I must acknowledge that M001 is a great course & I can also understand that any course cannot cover all the aspects. Still the course has made a brilliant attempt to cover all key aspects :slight_smile:

1 Like