【问题标题】:Query for obtain role privileges for each collection mongoDB查询获取每个集合 mongoDB 的角色权限
【发布时间】:2016-12-16 20:11:42
【问题描述】:

我正在寻找如何获得这样的东西:

{  _id: "myApp.appUser",
  role: "appUser",
  db: "myApp",
  privileges: [
       { resource: { db: "myApp" , collection: "" },
         actions: [ "find", "createCollection", "dbStats", "collStats" ] },
       { resource: { db: "myApp", collection: "logs" },
         actions: [ "insert" ] },
       { resource: { db: "myApp", collection: "data" },
         actions: [ "insert", "update", "remove", "compact" ] },
       { resource: { db: "myApp", collection: "system.js" },
         actions: [ "find" ] },
  ],
  roles: []
}

此 JSON 属于 MongoDB 文档 (https://docs.mongodb.com/v3.2/reference/system-roles-collection/#examples)。

但是我没有找到获取这个的查询。

我知道我能做到:

db.getSiblingDB('databaseName').getRole('rolename', { showPrivileges: true } )

但我不想要这个,因为我需要同一行中的所有权限(操作)。

谁能帮帮我?

谢谢!!

【问题讨论】:

    标签: mongodb


    【解决方案1】:

    您可以采取几种方法来查询数据集。 首先,您可以运行以下 rolesInfo 命令,该命令返回角色继承和在 myApp 数据库中定义的角色 appUser 的权限:

    db.runCommand(
        {
            "rolesInfo": { 
                "role": "appUser", 
                "db": "myApp" 
            },
            "showPrivileges": true
        }
    )
    

    另一种方法是查询 admin 数据库中的 system.roles 集合:

    use admin
    db.getCollection('system.roles').find({ "role": "appUser" })
    

    【讨论】:

    • 非常感谢您的回答。但是,结果与我通过初始查询获得的 JSON 非常相似。我想我必须用 bash shell 处理带有 JSON 的文本文件。所以我知道它不会退出任何方式来获取同一行中每个集合的操作?谢谢!!
    • 你是说exist吗?上述查询给出的 JSON 结果略有不同,但都有特权,我认为这回答了你的问题,因为你说 But I don't want this because I need all the privileges (actions) in the same line.
    • 除了同一行,现在不重要了。要点是按集合过滤答案。在 sql 中,它将是“select .... where collection =”。
    • 主要是按集合过滤答案。我只想为一个集合获取 JSON 答案。我想做这样的查询: db.getCollection('system.roles').find({ "role": "mdbuezbdc", "collection": "C_UEZBD_collection1" }) MongoDB 的答案是 "Fetched" 但是它没有回答询问的 JSON。
    猜你喜欢
    • 1970-01-01
    • 2021-03-02
    • 2014-02-03
    • 1970-01-01
    • 1970-01-01
    • 2019-07-15
    • 2015-09-30
    • 2016-12-23
    • 2014-05-07
    相关资源
    最近更新 更多