【问题标题】:Trying to add numbers within my MongoDB database尝试在我的 MongoDB 数据库中添加数字
【发布时间】:2021-10-06 19:48:03
【问题描述】:

所以我一直在尝试为我的经济/有趣机器人制作这个“工作”命令。没有运气将 5 个现金添加到数据库中...

@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}})

【问题讨论】:

  • 使用$inc 而不是$set
  • 试过了...但实际上什么也没发生。没有错误,MongoDB 数据库没有更新:\

标签: python mongodb discord discord.py bots


【解决方案1】:

您的数据库中没有任何更新,因为您的查询不同:

  • {"_id": ctx.author.id} → 你正在寻找一个 int id。
  • {"_id": f"{ctx.author.id}"} → 你正在寻找一个 str id

正如@Barmar 所说,您应该使用$inc 而不是$set
你也可以用update_many代替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}})        

【讨论】:

  • 非常感谢你们的解释和帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-11-16
  • 1970-01-01
  • 1970-01-01
  • 2021-05-09
  • 2021-02-18
  • 1970-01-01
  • 2017-04-29
相关资源
最近更新 更多