Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

335
Visualizações
How can i check if the value for "_id" field exists and how to read, write, update and delete data based on it

I have a discord bot for dealing with user notes. So the bot basically stores a string as a note in a list inside the database. The notes are user specific. I tried setting ctx.author.id as the value for "_id" field and then checking if the user id of the person using the command is equal to the value of "_id" in any document in the database. But it does not work.

Here is my code for the command:

@client.command()
async def makenotes(ctx, *, args):
    try:
        if (len(db.NoteBoyNotes.find_one({'_id':ctx.author.id})) == 0):
            NotesUpdateDict = {'_id':ctx.author.id, 'notes':[]}
            NotesUpdateDict['notes'].append(args)
            NBNotes.update(NotesUpdateDict)
            x = notesCol.insert_one(NBNotes)
            await ctx.send("Ok, Noted")
        else:
            pass
    except:
        await ctx.send("Some error occurred")

Can someone help with this? Another thing, I am using motor for asyncio, not pymongo.

EDIT: Here is the error: enter image description here

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

This answer is from a comment posted by @kpie,

@client.command()
async def makenotes(ctx, *, args):
    try:
        check = await db.NoteBoyNotes.find_one({'_id':ctx.author.id})
        if (check is None):
            NotesUpdateDict = {'_id':ctx.author.id, 'notes':[]}
            NotesUpdateDict['notes'].append(args)
            NBNotes.update(NotesUpdateDict)
            x = notesCol.insert_one(NBNotes)
            await ctx.send("Ok, Noted")
        else:
            pass
    except:
        await ctx.send("Some error occurred")```
over 4 years ago · Santiago Trujillo Relatório

0

Try this:

@client.command()
async def makenotes(ctx, *, args):
    try:
        check = awaut db.NoteBoyNotes.find_one({'_id':ctx.author.id})
        if (len(check) == 0):
            NotesUpdateDict = {'_id':ctx.author.id, 'notes':[]}
            NotesUpdateDict['notes'].append(args)
            NBNotes.update(NotesUpdateDict)
            x = notesCol.insert_one(NBNotes)
            await ctx.send("Ok, Noted")
        else:
            pass
    except:
        await ctx.send("Some error occurred")
over 4 years ago · Santiago Trujillo Relatório

0

You need to await db.NoteBoyNotes.find_one so that you get the returned value instead of the future.

Also I believe find one Returns a single document, or None if no matching document is found. so you check if check is None rather than len().

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda