There is two ways to update data using mongoose.
User.updateOne({username}, {$set: {age}})const user = await User.findById(userId)
user.age = age
user.save()
Which one is better and why?
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')