python - 如何使用 python 更新 MongoDB 文档?

标签 python mongodb pymongo discord.py

我想做的是使用 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/

相关文章:

node.js - 如何修改我的 mongodb 查询以使结果不包含存储在数组中的特定值?

mongodb - 按 ObjectId 日期聚合 MongoDB 结果

python - 无法使用 pymongo 2.2 连接到 MongoDB 2.0.5 数据库

python - Mongodb 组平均数组

Python 模块导入因未知原因失败

python - 如何从字典中获取值列表?

node.js - Mongoose:geoNear 并填充。使用executeDbCommand 时是否可以填充字段?

python - Python 夏令时前一周发出警报

python - numpy 线性方程的非零解

node.js - 如何在 Mongoose 的$set中使用$push?