Chapter 4: CRUD Optimization Lab 4.2: Aggregation Performance

On Compass I cannot run the following in Explain Plan Tab, why?
{ $match: { stars: { $gt: 2 } } }, { $sort: { stars: 1 } }, { $group: { _id: “$cuisine”, count: { $sum: 1 } } }

It gives me an error:
Error getting filter : Expected ‘filter’ to be BSON (or equivalent), but got primitive.A instead. Doc = [{find restaurants} {filter [[{$match [{stars [{$gt 2}]}]}] [{$sort [{stars 1}]}] [{$group [{_id $cuisine} {count [{$sum 1}]}]}]]} {maxTimeMS 5000} {returnKey false} {showRecordId false}]

Then, I cannot run on mongoShell the following command with explain()

db.restaurants.aggregate([
{ $match: { stars: { $gt: 2 } } },
{ $sort: { stars: 1 } },
{ $group: { _id: “$cuisine”, count: { $sum: 1 } } }
]).explain(“executionStats”)

It gives me an error as well.

How am i suppose to check for performance if everything gives me errors?
Help please!

Screenshot of the error you get in mongo shell.

Here is the screenshot:

I was able to get some results using this command:

db.restaurants.aggregate([ { $match: { stars: { $gt: 2 } } }, { $sort: { stars: 1 } }, { $group: { _id: “$cuisine”, count: { $sum: 1 } } }], {explain:true} )

However, how do i use explain with “executionStats”

Try this

db.collection.explain().aggregate(…)
db.collection.explain(‘executionStats’).aggregate(…)

1 Like

Hi
Myself Ayan, I am going through the videos sessions and the quiz section.
I have doubt on the Covered Queries quiz section, can you explain the 3rd and the 4th option in that quiz and why is the 4th one right

Hi @Ayan_Gorai,

I have initiated a discourse inbox thread with you. Please respond there if you have any further questions.

Kind Regards,
Sonali

Hey folks.
I tried with my index ({ stars: 1, cuisine: 1}), and “executionTimeMillis” : 553,
With official answer ({ stars: 1}) “executionTimeMillis” : 1295,
Who can tell me is it OK?
Am I doing something wrong? Or the answer is wrong?

You are missing one of the requirement of the lab:

Keep in mind that there might be several indexes that resolve this error, but we’re looking for an index with the smallest number of fields that resolves the error and services this command.

You index is part of the several indexes set that resolve the error, but it is not the one with the smallest number of fields.