【问题标题】:add roles to a user in mongodb via python (pymongo)通过python(pymongo)向mongodb中的用户添加角色
【发布时间】:2021-08-02 13:57:33
【问题描述】:

在向 mongo DB 中的现有用户添加新角色时,我需要帮助。我需要 Pymongo 中的解决方案。非常感谢您的帮助

我尝试使用 db._create_or_update_user() db._create_or_update_user( "UserName","password", roles= [ {"role": "read", "db": "databbase"} ])

但我收到错误消息,缺少 2 个必需的位置参数、密码和只读

【问题讨论】:

标签: python python-3.x mongodb pymongo pymongo-3.x


【解决方案1】:

您可以在 pymongo 中使用“原始”mongodb 命令。像这样:

from pymongo import MongoClient

client = MongoClient()

db = client.your_database_name

db.command("grantRolesToUser", "your_username", roles=["read"])

【讨论】:

  • 我如上使用但得到以下错误 - pymongo.errors.operationFailure: "user" is not a valid argument to grantRolesToUser, full error: {'ok': 0.0, 'errmsg': ' "user" 不是 grantRolesToUser' 的有效参数。 'code': 2, 'codeName': 'BadValue'}
  • 我删除了关键字 user 并且它有效 db.command("grantRolesToUser", "your_username", roles=["read"]))
  • 我的错,我编辑答案。如果对您有帮助,请将答案标记为已接受。
猜你喜欢
  • 1970-01-01
  • 2016-04-22
  • 2014-09-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-11
  • 2016-11-19
  • 2021-06-17
  • 1970-01-01
相关资源
最近更新 更多