Python cursor instances - mongodb

Viewed 2655

im currently trying to make a leaderboard command for discord, and im coming across an error when trying to get the data from my Mongo database.

    @commands.command()
async def leaderboard(self, ctx):
    rankings = levelling.find().sort("xp"), -1
    i = 1
    embed = discord.Embed(title="Rankings")
    for x in rankings:
        temp = ctx.guild.get_member(x["id"])
        tempxp = x["xp"]
        print(embed.add_field(name=f'{i}: {temp.name}', value=f'Total XP: {tempxp}', inline=False))
        i += 1
        if i == 11:
            break
    await ctx.send(embed=embed)

Command raised an exception: TypeError: index 'id' cannot be applied to Cursor instances

1 Answers
    rankings = levelling.find().sort("xp"), -1

The right hand side of this assignment specifies two elements. They get combined into an array which is what gets assigned to rankings.

When you subsequently iterate rankings, you are not iterating documents in the cursor but this array, hence x in your code is the cursor object.

Remove the , -1 part.

Related