【问题标题】:Can't pull information from MongoDB无法从 MongoDB 中提取信息
【发布时间】:2021-04-30 17:46:31
【问题描述】:

每次尝试从数据库中提取有关用户的信息都会返回 None

我的代码:

@bot.command()
async def warnings(ctx, user:discord.Member = None):
    client = MongoClient('connection')
    db = client.gettingStarted
    collection = db.Warning
    if user == None:
        await ctx.send('Please give a valid user')
    else:
        x = collection.find_one({"id":f"{user.id}"})
        print(x)

可能是什么问题?

【问题讨论】:

    标签: python python-3.x mongodb discord.py


    【解决方案1】:

    尝试改变

     x = collection.find_one({"id":f"{user.id}"})
    

     x = collection.find_one({"id": user.id})
    

    如果它不起作用,请告诉我。

    【讨论】:

    • 这个建议背后的直觉是什么?
    • @SimasJoneliunas 在使用 f 字符串时,将值更改为字符串,因此当 mongodb 中的值为整数时,他正在使用字符串查找“id”。所以基本上 x 将返回 None,因为 db 无法找到带有值字符串的“id”:user.id。
    猜你喜欢
    • 2014-09-08
    • 2019-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-08
    • 1970-01-01
    相关资源
    最近更新 更多