Question

I have a collection of documents that looks like:

[{ id : 1, a : 123, b : 342, name : 'test'}, { id : 2, a : 23, b : 32, name : 'another'}]

I am trying to sort over column a and then add another column to each document containing the rank of each value (where ties are allowed and the average is taken). It seems like I should be using the MongoDB aggregation framework, but I cannot figure out how to sort, and then assign the rank to another column. In the end I should end up with :

[
    { id : 1, a : 123, b : 32, name : 'test', aRank : 1, bRank : 1.5}, 
    { id : 2, a : 23, b : 32, name : 'another', aRank : 2, bRank : 1.5}
]

Any help? Thank you

Was it helpful?

Solution

Since no answer was ever given -- tied to searches bringing nothing up. I built an npm module that provides standard and fractional ranking for any js array over a numeric column.

I never did come up with a way of doing it with aggregation in mongodb, but this solution works well enough for me.

npm module: https://www.npmjs.org/package/rank.js

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top