是否可以使用PyMongo检查collection.find是否返回false?

问题描述 投票:1回答:1

我正在使用PyMongo通过discord.py制作基本的货币机器人,但是,用户可以多次在数据库中注册。

async def register(ctx):
    insert = {"userid":ctx.message.author.id,"cash":0}
    collection.insert_one(insert)
    await ctx.send('okie dokie you are registered')

这是register命令的代码,但是我不确定如何检查collection.find查询是否返回true或false。是否有人知道如何将其用作支票,或以任何方式检查他们是否已注册?

python pymongo discord.py
1个回答
0
投票

您可以使用find_one(),它将返回obj或无。

async def register(ctx):
    existing = collection.find_one({"userid": ctx.author.id})
    if not existing:
        # register the user
    else:
        await ctx.send("Sorry, you're already registered.")

参考:

  • [collection.find_one()-它声明“返回单个文档,如果找不到匹配的文档,则返回collection.find_one()。”
© www.soinside.com 2019 - 2024. All rights reserved.