D
D
Di2022-01-22 17:54:27
Python
Di, 2022-01-22 17:54:27

How to fix syntax error when aggregating (mongodb discord.py)?

I'm trying to create a command that displays a specific field of a random document in a collection:

@client.command()
async def name(ctx):
    db = cluster['dbname']
    collection = db['collname']
    await ctx.send(embed = discord.Embed(description = f"{collection.aggregate({$sample : {size:1}})['fieldname']}"))

But it gives me a syntax error, pointing to the closing parenthesis after size
What's the problem?
Help me please

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2022-01-22
@Vindicar

Try not to play around with one-liners and f-strings, but first calculate the required value in a separate variable, and then convert it to a string for Embed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question