MongoDB stats with Python

Hi everyone,

I working on Python as beginner and would like to display various statistical information on mydatabases: maximum, minimum, and average number of pages per category. I would like to use an aggregation pipeline, the $group keyword, and the appropriate accumulators. Plus “$unwind” for this problem.

First, does the use of db.command(“collstats”, “collection”) is correct? Any tips to achieve this result please?

Thank you.

Hi @William_Menant - welcome to the MongoDB community forums!

I’m going to guess from the structure of your question that you’re doing a database course of some kind and you’ve been set some homework.

The query you need to specify will depend on the structure of your data. I suspect that using collstats is the wrong approach. I think you’re more likely to want to do an aggregation query on your data where you select the appropriate documents, group by category and use the appropriate accumulators in that group stage to gather the statistics that you want.

The trick with aggregation queries is to build them up one stage at a time. Try creating a $group stage that will produce one group document per category, and then try to use the $max accumulator to get the maximum value you’re looking for. Once you’ve done that, writing the other accumulator expressions should be relatively straightforward.

I’m not sure why you’d need to use $unwind but if you read up on what that stage does, then I’m guessing it’ll become reasonably obvious once you get to that stage.

Best of luck with the exercise!


Hi @Mark_Smith - Thank you for your support.

You’re right, I’m just beginning into the MongoDB World and this is a kind of homework to learn it.
I 'mstuck with this and try to understand how to do and your help is really appreciate.

I will try to focus on the directions said and will back here if needed.
Again, thanks.

1 Like