Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

161
Visualizações
Compound Interest MongoDB Query

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 🙏

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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.

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda