簡體   English   中英

有沒有辦法使用 PyMongo 檢查 collection.find 是否返回 false?

[英]Is there a way to check if a collection.find returns false using PyMongo?

我正在使用 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。 有誰知道如何使用它作為支票,或任何方式來檢查他們是否已經注冊?

您可以使用find_one() ,它將返回 obj 或 None。

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() - 它聲明“返回單個文檔,如果沒有找到匹配的文檔,則返回None 。”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM