2

I want a user to be able to read and update, but not able to delete in any collection for MongoDB.

The command I used is:

db.createUser(
 {
 user: "user",
 pwd: "user",
 privileges: [
 { resource: { db: "icif_pattern" , collection: "" },
 actions: [ "find", "createCollection", "dbStats", "collStats" ] },
 ],
 roles: []
 }
)

But it throws an error:

Error: couldn't add user: "privileges" is not a valid argument to
createUser

I am following the information found in the following article:

Mongo user roles (MongoDB Docs)

John K. N.
18.9k14 gold badges56 silver badges117 bronze badges
asked Jun 7, 2018 at 9:30

1 Answer 1

2

As per MongoDB documentation here Collection-level access control allows administrators to grant users privileges that are scoped to specific collections.

Administrators can implement collection-level access control through user-defined roles. By creating a role with privileges that are scoped to a specific collection in a particular database, administrators can provision users with roles that grant privileges on a collection level.

Required Access

To create a role in a database, you must have:

  • the createRole action on that database resource.
  • the grantRole action on that database to specify privileges for the new role as well as to specify roles to inherit from.

Built-in roles userAdmin and userAdminAnyDatabase provide createRole and grantRole actions on their respective resources.

To create a role with authenticationRestrictions specified, you must have the setAuthenticationRestriction action on the database resource which the role is created.

use admin
db.createRole(
 {
 role: "myClusterwideAdmin",
 privileges: [
 { resource: { cluster: true }, actions: [ "addShard" ] },
 { resource: { db: "config", collection: "" }, actions: [ "find", "update", "insert", "remove" ] },
 { resource: { db: "users", collection: "usersCollection" }, actions: [ "update", "insert", "remove" ] },
 { resource: { db: "", collection: "" }, actions: [ "find" ] }
 ],
 roles: [
 { role: "readWrite", db: "admin" }
 ]
 },
 { w: "majority" , wtimeout: 5000 }
)

Roles

In the roles field, you can specify both built-in roles and user-defined roles.

{ role: "<role>", db: "<database>" }

For your further ref here , here and here

answered Jun 7, 2018 at 10:01

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.