0

At first - I am a beginner with mongodb. So i have next probleb. I am using such a model as below with mongoengine:

class Stats(Document):
    Name = StringField(max_length=250)
    timestamp = LongField(default=mktime(datetime.now().timetuple()))
    count = IntField()
    <some other fields>

What exactly I want is to filter by the name (it's clear) and use aggregation operation sum over field count. But I want to count the sum of records grouped by hours/days/months.

As example, if we have records with such timestamps [1532970603, 1532972103, 153293600, 1532974500], then 1-2 form first group, and 3-4 form second group.

And that is where I have stuck. I have some ideas about grouping by every n records, or by dividing timestamp on 3600 (1 hour = 3600 seconds), but how to make it with mongoengine. Or even how to insert some expressions with python in a pipeline?

I will very appreciate any help.

Yuriy Yosipiv
  • 53
  • 1
  • 7

1 Answers1

0

I would recommend to use ISO date format and store complete date in timestamp. Here is your model

class Stats(Document):
    Name = Document.StringField(max_length=250)
    timestamp = Document.DateTime(default=datetime.utcnow()) //ISO time format recommended
    count = Document.FloatField()
    meta = {'strict': False}

Now you can aggregate them accordingly.

Stats.objects.aggregate(
    {
        '$group': {
            '_id': {'year': {$year: '$timestamp'},
                    'month': {$month: '$timestamp'},
                    'day' : {$dayOfMonth: '$timestamp'},
                    'hour': {'$hour: '$timestamp'},
            }
        }
    }
)
Kartikeya Mishra
  • 118
  • 2
  • 10
  • 1
    Thanks for the answer! A few days later I've reached the same solution and forget about my question. For this case, it might be the only right way:) And since that time I became more friendly with mongo itself, so I can feel all power of the aggregation framework. – Yuriy Yosipiv Aug 30 '18 at 09:00