db.createRole with Mongoose

174 Views Asked by At

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?

1

There are 1 best solutions below

0
On BEST ANSWER

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: []
     });