首页 > 解决方案 > PyMongo 和 Discord.py 更新对象

问题描述

我正在尝试将 discord.py 与 PyMongo 集成,但现在我遇到了命令 update_one 的问题。 在此处输入图像描述

如您所见,“Corsi”是一种对象类型,我正在尝试使用这些命令向该对象添加字段。

@client.command(pass_context = True)
async def foo(ctx):

    author = ctx.author.id
    reg = {"_id": author}

    collection.update_one({"_id": author}, {"$set": {"Corsi": {'test1': 89}}})
    await ctx.message.channel.send("foo()")

    #accedo ad un obj



@client.command(pass_context = True)
async def foo2(ctx):

    author = ctx.author.id
    reg = {"_id": author}

    collection.update_one({"_id": author}, {"$set": {"Corsi": {'test2': 123}}})
    await ctx.message.channel.send("foo2()")

    #accedo ad un obj

当我使用命令 !foo2 时它工作正常,它将值 {test2:123} 添加到字段中,而当我在使用 !foo2 后使用命令 !foo 时,它将我的 {test2:123} 替换为 {test1:89}。

那么我怎样才能将新字段添加到对象中呢?

标签: pythondiscord.pypymongo

解决方案


推荐阅读