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

104
Visualizações
MongoDB update many using the values of the user

I've written a function to execute hourly which looks up a user and finds some values and then pushes those values into a history collection that records the hourly updated values. I've written this so far as a test just finding a user by their ID but now I need to roll this out to my entire database of 50,000+ users.

From what I've read using updateMany is a lot more performant but I'm not entirely sure how to retrieve the document detail of the record that is being updated at the time.

Here is my code so far, which you can see I'm first looking up the user and then grabbing their valuation details which I'd like to then push into a history collection.

exports.updateUserValuationHistoric = () => {
    User.find({ _id: "609961fdd989613914ef7216" })
        .populate('UserValuationHistory')
        .exec((err, userDoc) => {
            if (err){
                console.log('[ERROR]: ', err)
            }
            const updatedValuationHistory = {
                totalValuation: userDoc[0].valuation.totalValuation,
                comicsValuation: userDoc[0].valuation.comicsValuation,
                collectiblesValuation: userDoc[0].valuation.collectiblesValuation,
                omiValuation: userDoc[0].valuation.omiValuation
            }
            UserValuationHistory.findOneAndUpdate(
                { user: userDoc[0]._id },
                { $push: {
                    'history': updatedValuationHistory
                    }},
                {upsert: true, new: true}
                )
                .exec((error, updated) => {
                    if (error){
                        console.log('[ERROR]: Unable to update the user valuation history.')
                    } else {
                        console.log('[SUCCESS]: User historic valuation has been updated.')
                    }
                })
        })
}

Any help is greatly appreciated!

User model: https://pastebin.com/7MWBVHf3

Historic model: https://pastebin.com/nkTGztJY

about 4 years ago · Juan Pablo Isaza
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