我想做的是使用 python 和discord.py 更新 MongoDB 文档,但我放置的代码不起作用。
elif string2 == "add":
if string3 == "administrator":
cur = coll.find({"_id" : string1.id})
for doc in cur:
if doc["perm"] == "administrator":
await self.bot.say("Permission {} already found on db for user {}".format(string3, string1.name))
else:
db.users.update_one({"_id" : string1.id, "name" : string1.name, "perm" : "administrator"}, upsert=False)
await self.bot.say("Permissions updated on db for user {}".format(string1.name))
以下是错误。
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: update_one() missing 1 required positional argument: 'update'
用户收集的文档:
_id: "191598787410526208"
name: "Stevyb0t"
perm: "administrator"
最佳答案
基本上是其他人评论的内容,但进行了一些格式化以使其更清晰:
db.users.update_one(
{"_id": string1.id},
{"$set":
{"name": string1.name,
"perm": "administrator"
}})
我还删除了 upsert=False
因为这无论如何都是默认值,因此您不需要指定它 - 尽管明确地显示当然会有所帮助
编辑:阅读所有评论,已经提出建议将接受的评论作为答案,所以就在这里。我的回答和那条评论没有什么不同
关于python - 如何使用 python 更新 MongoDB 文档?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48722969/