Akio_
Akio_

Reputation: 11

Trying to add numbers within my MongoDB database

So I've been trying to make this "work" command for my economy/fun bot. No luck adding the 5 cash to the database...

@commands.command(aliases=["work"])
@commands.cooldown(1, 3600, commands.BucketType.user)
async def Work(self, ctx):
    results  = collection.find({"_id": ctx.author.id})
    for result in results:
        await ctx.send("Great job, you've been paid 5 cash.")
        collection.update_one({"_id": f"{ctx.author.id}"},{"$set":{"Cash": 5}})

Upvotes: 0

Views: 61

Answers (1)

MrSpaar
MrSpaar

Reputation: 3994

Nothing is updating in your database because your queries are different:

  • {"_id": ctx.author.id} → you're searching for an int id.
  • {"_id": f"{ctx.author.id}"} → you're searching for a str id

As @Barmar said, you should use $inc instead of $set.
You can also use update_many instead of update:

@commands.command(aliases=['Work'])
@commands.cooldown(1, 3600, commands.BucketType.user)
async def work(self, ctx):
    await ctx.send("Great job, you've been paid 5 cash.")
    collection.update_many({"_id": ctx.author.id}, {"$inc": {"Cash": 5}})        

Upvotes: 1

Related Questions