【问题标题】:mongodb does not push another object instead replaces the original object datamongodb 不推送另一个对象,而是替换原始对象数据
【发布时间】:2021-04-23 21:45:48
【问题描述】:

我在 discord.py 中创建了一个警告系统,并且我得到了要写入所需信息的实际数据,但是当我再次运行该命令时,它只会替换原始对象/警告中的数据。关键是每个警告都被视为数组中的一个对象,但它不会为新警告添加另一个对象

 @commands.command()
    @commands.guild_only()
    # @commands.has_guild_permissions(kick_members=True)
    async def warn(self, ctx, user: discord.User, *, reason=None):
        if user is None:
            await ctx.send("You have not provided a user!")
            return

        if user is user.bot:
            await ctx.send(f"<@{ctx.author.id}, you cannot warn a bot!")
            return

        if user.id == ctx.author.id:
            embed = discord.Embed(description=f"<@{ctx.author.id}>, you are not allowed to warn yourself!")

            embed.set_author(name=ctx.author.display_name, icon_url=ctx.author.avatar_url)
            embed.set_footer(text=f"{ctx.message.guild.name} Custom Designed Bot")
            embed.timestamp = ctx.message.created_at

            await ctx.send(embed=embed)
            return

        reason = reason or None
        _id = await self.bot.warns.find_by_id({"_id": user.id})
        first_warning = True

        warn = {
            "_id": user.id,
            "guild": ctx.message.guild.id,
            "warn": [{
                "PunishType": 'warn',
                "Moderator": ctx.message.author.id,
                "Reason": reason,
            }]
        }
        if first_warning:
            embed = discord.Embed(description=f"<@{ctx.author.id}>, has warned <@{user.id}> for {reason}")

            embed.set_author(name=ctx.author.display_name, icon_url=ctx.author.avatar_url)
            embed.timestamp = ctx.message.created_at
            await ctx.send(embed=embed)
            await self.bot.warns.upsert(warn)
            return first_warning is False
        elif first_warning is False:
            warns = await self.bot.warns.find_by_id({"_id": user.id})["warns"]
            newwarn = [{
                "PunishType": "warn",
                "Moderator": ctx.author.id,
                "Reason": reason,
            }]
            warns.append(newwarn)
            await self.bot.warns.update(
                {"_id": "user.id"},
                {"$push": {"warns": warns}})

【问题讨论】:

    标签: arrays python-3.x mongodb discord.py-rewrite


    【解决方案1】:

    如果你有一个带有 pymongo 的数组。

    您必须像await self.bot.warns.find_by_id({"_id": user.id}) 一样获取数据库对象获取数组。执行array.append(newwarn),然后不要用newwarn 更新warn,而是用数组替换newwarn

    
     warn = {
                "_id": user.id,
                "guild": ctx.message.guild.id,
                "warns": [{
                    "PunishType": 'warn',
                    "Moderator": ctx.message.author.id,
                    "Reason": reason,
                }]
            }
    
    await self.bot.warns.upsert(warn)
    
    warns = await self.bot.warns.find_by_id({"_id": user.id})["warns"]
    newwarn = [{
                "PunishType": "warn",
                "Moderator": ctx.author.id,
                "Reason": reason,
    }]
    warns.append(newwarn)
    await self.bot.warns.update(
                    {"_id": "user.id"},
                    { "$push" : {"warns": warns}})
    

    【讨论】:

    • 我已经插入了你给我的代码,它仍然只是替换它。我已经用整个命令更新了我的代码,以防万一有什么阻碍它。唯一的事情是当我删除我的 if 语句时,协程对象在 warns = await 上不可下标
    • 当您使用插入文件制作文档时,您需要在其中添加警告@Jango DarkWind
    • 我不明白我道歉
    • 当您使用 insert_one 制作文档时,您需要在该对象中添加 warns 以使其成为 @Jango DarkWind
    • 我没有在任何地方使用 insert_one
    猜你喜欢
    • 1970-01-01
    • 2021-12-28
    • 2018-07-27
    • 1970-01-01
    • 1970-01-01
    • 2012-12-05
    • 2022-06-27
    • 1970-01-01
    • 2019-06-02
    相关资源
    最近更新 更多