Cannot understand Lecture #2 from Chap 3 when implementing the demo

Hi, the video shows Accumulators such as $max, $min, etc. in $project stage in the pipeline, but the docs say something different with respect to the way those Accumulators behave in $project.

This is the best place to look https://docs.mongodb.com/manual/reference/operator/aggregation/max/

Hi @Sunanda_Samaddar_30067,

You can refer to the following section of the documentation for more details on accumulators:

Please let me know, if you have any questions.

Thanks,
Sonali

Please do what you just asked me to do, and go back to the video to be sure.

Hi @Sunanda_Samaddar_30067,

Accumulators are mainly used in the $group stage where it applies the operation to each document in a group of documents.

However, the behaviour of accumulators is different in the $project stage. The accumulator expressions within the $project stage work over an array within the given document.

To understand well about accumulators you can refer to the Accumulators section in the above post:

To understand how the accumulators work with other stages (specifically with $project), please refer to the examples for each individual accumulator operators:

If there is any other point, left unexplained, please let me know the exact point of confusion and we can work further to resolve it.

Thanks,
Sonali

Yes, but an example in the video shows an aggregation across documents over a field in $project; not above.

Hi @Sunanda_Samaddar_30067,

In the video, the example explained shows following queries:

db.icecream_data.aggregate([ { $project: { _id: 0, max_high: { $max: “$trends.avg_high_temp” } } } ])

db.icecream_data.aggregate([ { $project: { _id: 0, max_low: { $min: “$trends.avg_low_temp” } } } ])

Here the icecream_data collection contains documents with trends array (trends is an array of objects). So $max and $min will fetch the maximum avg_high_temp and minimum avg_low_temp respectively from the trends array.

Please let me know, if you have any questions.

Thanks,
Sonali

1 Like

The array of objects is causing a confusion plus the fact that the collection only contains one document because at quick glance it does look like multiple documents.

I think it would have been more obvious if the the trends field was a simple array of values and the collection had more than one document.

Or the Curriculum Engineer could briefly explain the structure of the document before proceeding with the lecture.

2 Likes

A screenshot might help @Sunanda_Samaddar_30067

3 Likes

Hi @007_jb,

Thanks for clarifying!!

@Sunanda_Samaddar_30067 you can also look at the structure of the aggregations.icecream_data collection in Compass or Mongo Shell.

Please let me know, if you have any questions.

Thanks,
Sonali