T
T
tatsuki12021-11-06 20:21:59
Python
tatsuki1, 2021-11-06 20:21:59

MongoDB problem, help?

I am writing one system with a MongoDB database, and I want that when the database by server ID is empty, data is entered, and when the server ID matches the server ID that is recorded in the database, the data is replaced, not written again.

@commands.command()
    async def welcome(self, ctx, channel: discord.TextChannel):
        if ctx.message.author.guild_permissions.manage_messages:
            result = channel1.find_one({"guild_id": ctx.guild.id, "channel_id": channel.id})
            if result is None:
                channel1.insert_one({f"guild_id": ctx.guild.id, "channel_id": channel.id})
                await ctx.send(f'{ctx.author.mention}, канал успешно установлен на {channel.mention}.')
            elif result is not None:
                channel1.update_one({"guild_id": ctx.guild.id}, {"$set": {"channel_id": channel.id}})
                await ctx.send(f'{ctx.author.mention}, канал изменён на {channel.mention}.')
        else:
            await ctx.send(f'{ctx.author.mention}, вам нужны права на управление сообщениями.')

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vindicar, 2021-11-06
@Vindicar

The update() method won't work?

R
Roman Mirilaczvili, 2021-11-07
@2ord

In my opinion, instead of

result = channel1.find_one({"guild_id": ctx.guild.id, "channel_id": channel.id})
            if result is None:
                channel1.insert_one({f"guild_id": ctx.guild.id, "channel_id": channel.id})
                await ctx.send(f'{ctx.author.mention}, канал успешно установлен на {channel.mention}.')
            elif result is not None:
                channel1.update_one({"guild_id": ctx.guild.id}, {"$set": {"channel_id": channel.id}})
                await ctx.send(f'{ctx.author.mention}, канал изменён на {channel.mention}.')

Just update_onespecify in the request options upsert=True.
https://docs.mongodb.com/manual/reference/method/d...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question