【问题标题】:Not a valid ObjectId PyMango不是有效的 ObjectId PyMango
【发布时间】:2021-12-28 09:10:56
【问题描述】:

我正在使用 PyMongo 作为我的不和谐机器人的数据库。 我正在制作一个黑名单系统,其中机器人不运行黑名单用户给出的命令。

这是我的代码:

@bot.event
async def on_message(message):
    author_id = message.author.id
    id = str(author_id) 
    objInstance = ObjectId(id)
    collection.find_one({"_id": ObjectId(id)})
    if True:
        await message.channel.send("You have been blacklisted from using the bot commands")
    else:
        await bot.process_commands(message)


@bot.command(aliases=["bl"])
async def blacklist(ctx, * ,member: discord.Member):
    if member is None:
        await ctx.send("Mention a member to blacklist them!")
    else:
        post = {"_id": member.id}
        collection.insert_one(post)

我收到此错误:

line 38, in _raise_invalid_id
    raise InvalidId(
bson.errors.InvalidId: '851756211841925141' is not a valid ObjectId, it must be a 12-byte input or a
24-character hex string

【问题讨论】:

    标签: python discord.py pymongo


    【解决方案1】:

    根据错误,使用ObjectId()时,参数必须匹配与MongoDB的ObjectId类型对齐的格式。

    也许你打算这样做:

    collection.find_one({"_id": id})
    

    https://docs.mongodb.com/manual/reference/method/ObjectId/

    作为旁注,避免将id 作为变量名,因为id 是一个内置函数。

    【讨论】:

      猜你喜欢
      • 2014-10-15
      • 2020-11-02
      • 1970-01-01
      • 2012-08-12
      • 2015-04-30
      • 1970-01-01
      • 2018-12-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多