MongoDB: limiting results from a $ gt request (from pymongo)

I collect statistics from a web service and save it in a collection. The data is similar to this (but with lots of fields):

{"downloads": 30, "dt": "2010-02-17T16:56:34.163000"}
{"downloads": 30, "dt": "2010-02-17T17:56:34.163000"}
{"downloads": 30, "dt": "2010-02-17T18:56:34.163000"}
{"downloads": 30, "dt": "2010-02-17T19:56:34.163000"}
{"downloads": 30, "dt": "2010-02-17T20:56:34.163000"}
{…}
{"downloads": 30, "dt": "2010-02-18T17:56:34.163000"}
{"downloads": 30, "dt": "2010-02-18T18:56:34.163000"}
{"downloads": 30, "dt": "2010-02-18T19:56:34.163000"}
{"downloads": 30, "dt": "2010-02-18T20:56:34.163000"}

If someone requests daily numbers for the last thirty days, this will mean the maximum amount (in this example) of 'downloads' pr. day. This is the last record of the day.

Using collection.find({"dt": {"$gt": datetime_obj_30_days_ago}}), of course, I get all the lines, which is not very suitable. Therefore, I am looking for a way to return only the last day for a given period.

I was told what group()might be the way, but I can’t figure out how to make it work on this instance.

Any tips, pointers would be much appreciated!

+3
source share
1

, group. javascript ( ), datetime. :

db.coll.group(
    key='function(doc) { return {"dt": doc.dt.toDateString()} }',
    condition={'dt': {'$gt': datetime_obj_30_days_ago}},
    initial={'downloads': 0},
    reduce='function(curr, prev) { prev.downloads = Math.max(curr.downloads, prev.downloads) }'
)

, , , . , .

+1

Source: https://habr.com/ru/post/1733398/


All Articles