how to calculate xirr using aggregation at db level only
I don’t believe you can do this with aggregation alone… you need a recursive root-finding algo… which I don’t believe is possible with agg alone. Not sure what language / frameworks you’re using… but you can use Python’s scipy.optimize newton
to achieve this.
Recursive function calls are not possible in aggregation pipeline. However, according to Church-Turing thesis you can write any recursive function as iteration. In aggregation pipeline you can do only Collection-controlled loops (with { $map: {...} }
) or Count-controlled loops (with { $map: { input: { $range: [...] } } }
). What you can do, is to assume a max. number of iterations and skip iteration when condition is met.
I created a story in medium showing a possible solution: MongoDB Aggregation Pipeline: Approximation with Newton’s Method | by Wernfried Domscheit | Feb, 2025 | Medium
Best Regards
Wernfried
Thanks for the help.
Thanks for the help . I will try to apply these to calculations .