Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

239
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!