Reputation: 87
I'm new to Mongoose and have am trying to do something like this:
I'm able to get this working as follows:
const doc = await Foo.findById(id)
// Do stuff
const updatedDoc = await Foo.findOneAndUpdate(
{ _id: id},
{ $inc: { counter: 1 },
{ new: true }
)
But this doesn't feel like it's the right way to do it since I have to find the document twice. What is the idiomatic way to do this?
Upvotes: 0
Views: 34
Reputation: 287
you can do like
const doc = await Foo.findById(id)
// Do stuff
doc.counter++
doc.save()
Upvotes: 1