Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

111
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda