Skip to content
Advertisement

Aggregation $match within a $sum

I was wondering if it was possible to somehow use the $match operator within the $sum function for aggregation.

JavaScript

I want to be able to use the value of the $sum operator within the project fields somehow, I just don’t really understand what the right approach would be for this.

Sample Input (may be too long): https://www.toptal.com/developers/hastebin/ixamekaxoq.json

Sample Output: ( 2 * FIGHT_PROP_CRITICAL ) + FIGHT_PROP_CRITICAL_HURT sorted from highest to lowest for each item.

JavaScript

Advertisement

Answer

There are still a few ambiguities about how you want to aggregate your data, but using the full document from your link, here’s one way to produce the output you want.

N.B.: Weapons in the "equipList" don’t have "reliquarySubstats" so they show a "statValue" of null in the output.

JavaScript

Try it on mongoplayground.net.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement