Search code examples
node.jsmongodbmongoosemongodb-roles

db.createRole with Mongoose


As per documentation of Mongoose, there is no way to create a role with MongoDB.

I want to perform role creation with MongoDB

db.createRole()

Though, I could see some external libraries doing a similar approach but not exactly the same as above and also some of those are not maintained

mongoose-authorization

mongoose-permission

mongoose-role

Any best approach for this?


Solution

  • In node js you can achieve it like this

    await db.command({
             createRole: 'newTestRole', 
             privileges: [
                 { resource: { role: "read",  db: 'dbname', collection: "my_collection" }, actions: ["find"] }
            ],
             roles: []
         });