如何删除 MongoDB 中用户的 dropCollection 访问权限?

How to remove dropCollection access right from users in MongoDB?

创建了一个具有 readWrite 集合访问权限的 MongoDB 用户。但我不想向该用户授予 dropCollection 访问权限。我该如何实施。

谢谢。

您可以define your custom role满足您的目的。

您还可以通过继承自定义角色的 admin.system.roles.roles 字段来扩展现有角色。

这是一个自定义角色示例

{
  role: "YourRole",
  db: "YourDatabase",
  privileges:
      [
          {
              resource: {db: "YourDatabase", collection: "OneOfCollections"},
              actions: ["find", "insert", "update", "remove", "createIndex" ...]
          },
          {
              resource: {db: "YourDatabase", collection: "AnotherCollection"},
              actions: ["find", "insert", "update", "remove", "createIndex" ...]
          },
          .
          .
          .
      ],
  roles:
      [
          { role: "RoleYouWantToInherit", db: "TheDatabaseRoleIsDefinedFor" },
          ...
      ]
}

@n9code

运行 对管理员集合的查询出现错误。

db.runCommand({
    createRole:"mynew",
    priviliges : [..],
    roles: [
    { role: "read", db: "admin" }
  ],
  writeConcern: { w: "majority" , wtimeout: 5000 }
})

显示 "error msg:no such cmd creareRole"