Home > OS >  How can I add 1 to a value in a MongoDB?
How can I add 1 to a value in a MongoDB?

Time:10-03

When using findOneAndUpdate how can I add one to a value? In this case, I want to add 1 to the commandsUsed Object. Example Below:

await mongo().then(async (mongoose) => {
   try {
      await userSchema.findOneAndUpdate({
         _id: author.id,
      }, {
         _id: author.id,
         commandsUsed: //I want to add 1 to this Object
      }, {
         upsert: true,
  })

CodePudding user response:

You can use findOne then add the field, finally save it.

const doc = await userSchema.findOne({ _id: author.id })
doc.commandsUsed  
doc.save();
  • Related