Mongodb - how to calculate percentiles

I have access logs as shown below which are stored in the mongodb instance:

Time Service Latency [27/08/2013:11:19:22 +0000] "POST Service A HTTP/1.1" 403 [27/08/2013:11:19:24 +0000] "POST Service B HTTP/1.1" 1022 [27/08/2013:11:22:10 +0000] "POST Service A HTTP/1.1" 455 

Is there an analytics function like PERCENTILE_DISC in Oracle to calculate the percentile? I would like the calcualte latency percentile over a period of time.

+4
source share
1 answer

There is still no own way to calculate percentiles, but by combining several aggregated operators, you can get the same result.

 db.items.aggregate([ {'$group': { '_id': { 'league': '$league', 'base': '$base', 'type': '$type' }, 'value': {'$push': '$chaosequiv'} }}, {'$unwind': '$value'}, {'$sort': {'value': 1}}, {'$group': {'_id': '$_id', 'value': {'$push': '$value'}}}, {'$project': { '_id': 1, 'value': {'$arrayElemAt': ['$value', {'$floor': {'$multiply': [0.25, {'$size': '$value'}]}}]} }} ], allowDiskUse=True) 

Note. I wrote my source code in pymongo for a problem that should have been grouped into 3 fields in the first group, so this can be more complicated than needed for a single field. I would write a solution specific to this question, but I do not think that there is enough specific information.

+5
source

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


All Articles