Mongoose 找到文档后更新文档的惯用方法?

Mongoose 找到文档后更新文档的惯用方法?,mongoose,Mongoose,我是猫鼬的新手,我一直在尝试这样做: 拿到文件 做点什么 更新并保存文档 我可以按如下方式使其工作: const doc = await Foo.findById(id) // Do stuff const updatedDoc = await Foo.findOneAndUpdate( { _id: id}, { $inc: { counter: 1 }, { new: true } ) 但我觉得这不是正确的方法,因为我必须两次找到文档。这样做的惯用方法是

我是猫鼬的新手,我一直在尝试这样做:

拿到文件 做点什么 更新并保存文档 我可以按如下方式使其工作:

const doc = await Foo.findById(id)
// Do stuff
const updatedDoc = await Foo.findOneAndUpdate(
      { _id: id},
      { $inc: { counter: 1 },
      { new: true }
)
但我觉得这不是正确的方法,因为我必须两次找到文档。这样做的惯用方法是什么?

你可以这样做

const doc = await Foo.findById(id) 
// Do stuff
doc.counter++
doc.save()