mongodb 不推送另一个对象而是替换原始对象数据

mongodb does not push another object instead replaces the original object data

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

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

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

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


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