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

241
Vistas
What is the best practice for updating data via mongoose?

There is two ways to update data using mongoose.

  1. Using model methods, for example User.updateOne({username}, {$set: {age}})
  2. Find document, then change its properties and save it. Like this:
const user = await User.findById(userId)
user.age = age
user.save()

Which one is better and why?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

For .save() method it is actually pulling data from server(MongoDB) to client(Mongoose) and updating the data in memory and then applying the query in server. Using direct update or updateOne will perform query directly on server.

So make sure to use queries as much as possible. You can manually check by doing console.time in both the cases like

console.time('query')
const user = await User.findById(userId)
user.age = age
await user.save()
console.timeEnd('query')

and then do this

console.time('query')
await User.updateOne({ _id: userId }, { $set: { age } })
console.timeEnd('query')
about 4 years ago · Juan Pablo Isaza Denunciar
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