Home > OS >  MongoDb not adding properly
MongoDb not adding properly

Time:10-16

I am trying to add a number to an item in a document in MongoDB

I have this code:

            const schema = require('./commands/serverstats')
            const data = await schema.findOne({ Main:'Main' })
  data.TotalBobuxGiven  = Number(args[0])
  data.TotalRobuxGiven  = Number(args[1])
  await data.save()
  message.reply(`Done`)
    console.log('done')

It doesn't return done or it doesn't add the data properly. The schema is correct, and I am using the correct variables. There are no errors. What am I doing wrong?

CodePudding user response:

Query

  • try update, and 1 query, making the update faster atomic and safe

*i dont use mongoose, but you dont need find and save, update is the right way to do it

Test code here

db.collection.update({
  Main: "Main"
},
{
  $inc: {
    "TotalBobuxGiven": Number(args[0]),
    "TotalRobuxGiven": Number(args[1])
  }
})
  • Related