复利 MongoDB 查询

发布于 01-20 15:41 字数 814 浏览 2 评论 0原文

尝试计算我的聚合查询中的复利。

我创建了这个每周回报系数数组,假设起始值为 100 美元,

{

"investThis": 100,

"wklyReturns" : [ 
        0.953268776798189, 
        1.00703473704638, 
        0.994451681381773, 
        1.05357596706145, 
        0.99028490888956, 
        0.981037037037037, 
        1.01519025417356, 
        0.990515443440262, 
        1.01512249218671, 
        1.01603072983355, 
        0.991167064317986, 
        0.919182492650833, 
        0.991854491599945, 
        0.971319397617442, 
...
]

}

所需要做的就是将“investThis”乘以索引 0 回报系数,然后重用结果并将其乘以索引 0+1 回报系数,一直如此在每周退货列表中。

以下是 Google 表格计算的几个屏幕截图...

开始拖放

最终结果

非常感谢任何帮助

Trying to calculate compound interest in my aggregation query.

I have created this array of weekly return factors, and assuming a starting value of $100

{

"investThis": 100,

"wklyReturns" : [ 
        0.953268776798189, 
        1.00703473704638, 
        0.994451681381773, 
        1.05357596706145, 
        0.99028490888956, 
        0.981037037037037, 
        1.01519025417356, 
        0.990515443440262, 
        1.01512249218671, 
        1.01603072983355, 
        0.991167064317986, 
        0.919182492650833, 
        0.991854491599945, 
        0.971319397617442, 
...
]

}

All need to do is multiply "investThis" by index 0 return factor, then reuse the result and multiply that by index 0+1 return factor, all the way down the weeklyReturns list.

Here are a couple screenshots from google sheet calculation...

start drag and drop

final result

Any help is greatly appreciated ????

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忘羡 2025-01-27 15:41:25

如果我正确理解的话,您需要做的就是再添加一个步骤:

{
    $project: {
      "results": {
        $reduce: {
          input: "$wklyReturns",
          initialValue: "$investThis",
          in: {
            $multiply: [
              "$value",
              "$this"
            ]
          }
        }
      }
    }
  }

正如您在
$降低操作允许您每次使用先前的结果重复操作。

If I understand correctly, all you need to do is add your aggregation one more step:

{
    $project: {
      "results": {
        $reduce: {
          input: "$wklyReturns",
          initialValue: "$investThis",
          in: {
            $multiply: [
              "$value",
              "$this"
            ]
          }
        }
      }
    }
  }

As you can see on the playground.
The $reduce operation allows you to repeat an action each time using the previous result.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文