Re: Aggregate framework - how to calculate median?

964 views
Skip to first unread message

Stephen Steneker

unread,
Feb 18, 2013, 9:51:07 PM2/18/13
to mongod...@googlegroups.com
 
Are there any good examples how to calculate median in MongoDB using aggregate framework?
I have grouped my data already:

db.collection.aggregate( {$sort:{n:1}}, { $group:{ _id:"$_id",n:{ $push:"$n" }, len:{$sum:1} }} );

{_id: 2, n: [1,1,2,3,3,3,4,5,6,7], len:9},
{_id: 3, n: [1,5,5,6,6,7,9], len: 7} 

Can I use $splice function in aggregation framework?

Hi Jaan,

There is an open feature request to add a $median accumulator, if you would like to watch/vote on this:
 https://jira.mongodb.org/browse/SERVER-4929

A related request is for an expression to return the size of an array in projections:
 https://jira.mongodb.org/browse/SERVER-4899

The current workaround would be to calculate the median in your application code.

Cheers,
Stephen
Reply all
Reply to author
Forward
0 new messages