Yuriy Yosipiv
Yuriy Yosipiv

Reputation: 63

sum by hours/days/month based on timestamp in mongoengine/pymongo

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.

Upvotes: 0

Views: 176

Answers (1)

Kartikeya Mishra
Kartikeya Mishra

Reputation: 126

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'},
            }
        }
    }
)

Upvotes: 0

Related Questions