Aggregate function – using $project and $group

If you want the aggregated data to be stored in a specific column format then use $project.

Here is an example:-

pipeline1 = [
                            {'_id': {
                                'device_id': '$device_id',
                                'date': {'$dateToString': {'format': '%Y-%m-%d', 'date': '$timestamp'}},
                                'avg_v': {'$avg': "$value"},

                                '_id': 0,
                                'device_id': '$_id.device_id',
                                'avg_value': '$avg_v',
                                'min_value': '$min_v',
                                'max_value': '$max_v',
                                'date': '$'

The aggregated data structure will be as shown below in database:-

_id: 61af14c283751487558e8494
device_id: "DT002"
avg_value: 23
min_value: 19
max_value: 26
date: "2020-12-05"

Published by Seema Nair

I am a software professional, I blog occasionally, love photography, nature and often wonder about life!