In MongoDB, what do the built-in roles read, readWrite, dbAdmin and clusterAdmin each allow a user to do?
answer
- scope first: database or deployment
- data, administration, or operations
- readWrite reaches further than documents
- dbAdmin has no find
- clusterAdmin combines three cluster roles
basics
~20 sIn MongoDB, read queries one database's collections, and readWrite also changes them, including dropping collections. dbAdmin handles indexes, collection settings and statistics without find. clusterAdmin, held in admin, runs the deployment and has no find on application collections.
solid answer
~40 sA role grants actions on resources, and these four differ first by scope. `read` gives `find`, `listCollections`, `listIndexes`, `collStats` and `changeStream` on the non-system collections of the database named in the grant. `readWrite` adds `insert`, `update` and `remove`, but also `createIndex`, `dropCollection` and `renameCollectionSameDB`, so a service holding it can drop its own collections, though not the database. `dbAdmin` covers `collMod`, index work, `compact`, `validate`, profiling and `dropDatabase`, yet has no `find` on ordinary collections. `clusterAdmin` exists only in `admin` and combines `clusterManager`, `clusterMonitor` and `hostManager`: replica set configuration, `serverStatus`, `killop`, `shutdown`. It has no `find` on application collections, but it can drop ordinary databases. So a checkout service gets `readWrite` at most, an analytics job gets `read`, and an on-call DBA gets a cluster role rather than the old admin login.
code
javascript · 6 linesuse admin
db.createUser({
user: "analytics",
pwd: passwordPrompt(),
roles: [{ role: "read", db: "shop" }]
})go deeper
Recall the four scopes: read and readWrite for data in one database, dbAdmin for that database's administration, clusterAdmin for the deployment. Say that a grant names its database.
Explain the surprises in each action list: readWrite carries dropCollection but not collMod, dbAdmin has no find on ordinary collections, and clusterAdmin combines clusterManager, clusterMonitor and hostManager.
Show that you read action lists before granting. Point out that clusterAdmin can drop ordinary databases and that its read on local exposes the oplog, so it is not a harmless operations role.
Treat built-ins as a starting point with known excess. Decide where that excess is acceptable, such as analytics on read, and where a narrower custom role earns its upkeep, such as a service that should never drop collections.
## How a built-in role is scoped A MongoDB **role** is a named bundle of **privileges**, and a privilege is a resource plus the actions allowed on it. A user has no access beyond the roles it holds. Four built-in roles cover most of what a team reaches for, and the first thing that separates them is scope: - `read`, `readWrite` and `dbAdmin` exist in every database and apply to the database named in the grant, so `{ role: "read", db: "shop" }` reaches `shop` and no other database. - `clusterAdmin` exists only in the `admin` database and acts on the deployment as a whole. - The database a user is defined in does not cap its reach: a user created in `admin` can hold `read` on `shop`. The database roles work on **non-system** collections. System collections are those in `<database>.system.*` and `local.replset.*`. `read` and `readWrite` also name `system.js` explicitly, and `dbAdmin` names `system.profile`. ## What each role grants | Role | Scope | Main actions | Not included | |---|---|---|---| | `read` | one database | `find`, `listCollections`, `listIndexes`, `collStats`, `dbStats`, `changeStream` | any write, `listDatabases` | | `readWrite` | one database | all of `read`, plus `insert`, `update`, `remove`, `createCollection`, `createIndex`, `dropIndex`, `dropCollection`, `renameCollectionSameDB`, `convertToCapped` | `collMod`, `dropDatabase` | | `dbAdmin` | one database | `collMod`, `createIndex`, `dropIndex`, `compact`, `validate`, `enableProfiler`, `dropCollection`, `dropDatabase`, `collStats` | `find` on non-system collections, user management | | `clusterAdmin` | deployment, granted in `admin` | everything in `clusterManager`, `clusterMonitor` and `hostManager`, plus `dropDatabase` on ordinary databases | `find` or `insert` on application collections, user management | ## The parts people get wrong - **`readWrite` is more than data changes.** It carries `dropCollection`, so a service holding it can destroy its own collections. It does not carry `collMod`, so changing a validator or a TTL through that command is refused. - **`dbAdmin` does not read your data.** Among collections, its `find` covers only `system.profile`. The role that combines reading and administering a database is `dbOwner`, which inherits `readWrite`, `dbAdmin` and `userAdmin`. - **`clusterAdmin` is not harmless to data.** It holds `dropDatabase` on every database except `local` and `config`. Through `clusterMonitor` it also inherits `read` on `local`, and the oplog is a collection there whose entries carry written values. - **`read` is more than queries.** It includes `changeStream` on its database, so a job holding only `read` can follow changes as well as query them. It has no `listDatabases`, which is why such a user sees only the databases it holds privileges on. - **The three cluster roles split duties.** `clusterMonitor` holds the observing actions such as `serverStatus`, `inprog`, `top` and `replSetGetStatus`. `hostManager` holds `killop`, `setParameter`, `fsync` and `shutdown`. `clusterManager` holds `replSetConfigure`, `addShard` and `setFeatureCompatibilityVersion`. ## Three questions before granting Asking these catches most over-grants, whichever role you end up with: 1. Is the target one database, several, or the deployment itself? 2. Does the holder need to read documents, change documents, or change structure? 3. What else comes with the role, and would you accept a stolen credential using it? ## Choosing for a shared replica set Picture one self-managed replica set shared by a checkout service, a read-only analytics job and an on-call DBA, all moving off a single admin login: 1. Give the checkout service `readWrite` on `shop` if you need a quick, safe-enough step, and know that it can still drop collections. A custom role is the tighter end state. 2. Give the analytics job `read` on `shop`. If it really spans databases, `readAnyDatabase` is the wider version. 3. Give the on-call DBA `clusterMonitor` for inspection. Killing an operation needs `killop`, which lives in `hostManager` beside `shutdown`, so either accept that or add `killop` through a custom role. 4. Lock the old admin login away. It should not be anyone's daily credential. ## The wider family - **All-database roles.** `readAnyDatabase`, `readWriteAnyDatabase`, `dbAdminAnyDatabase` and `userAdminAnyDatabase` exist only in `admin`. They apply to every database except `local` and `config`, and they add `listDatabases` on the cluster. - **`userAdmin`.** It holds `createUser`, `createRole` and `grantRole` on its database, which lets its holder grant themselves anything there. On `admin`, that means the whole deployment. - **`root`.** It combines `clusterAdmin`, `userAdminAnyDatabase`, `dbAdminAnyDatabase`, `readWriteAnyDatabase`, `backup`, `restore` and more. These lists grow between releases, so read the role itself rather than a memorised list: ```javascript use shop db.getRole("readWrite", { showPrivileges: true }) ```
- Which built-in role fits an on-call DBA who must inspect operations and kill a runaway one, but must not reconfigure the replica set?No single built-in draws that line. `clusterMonitor` gives `inprog`, `serverStatus` and `replSetGetStatus`, but no `killop`. `killop` sits in `hostManager` beside `shutdown` and `setParameter`. `clusterAdmin` adds `replSetConfigure` too, which is what you wanted to avoid. The narrowest answer is a custom role in `admin` that inherits `clusterMonitor` and adds `killop` on `{ cluster: true }`.
- What do the AnyDatabase roles add over their single-database versions, and what do they still not reach?`readAnyDatabase`, `readWriteAnyDatabase` and `dbAdminAnyDatabase` give the same actions across all databases and add `listDatabases` on the cluster. They are defined only in `admin`, and they stop short of `local` and `config`. They grant database resources, not cluster administration, so `readWriteAnyDatabase` still cannot run `serverStatus`.
- How can a user holding only read on shop see that database in listDatabases without the listDatabases action?Without the action, `listDatabases` with `authorizedDatabases` unspecified or `true` returns the databases the user has privileges on, including those where the privileges are on specific collections. So the user sees `shop` and not the rest of the deployment.
- readWrite only changes documents, so a service holding it cannot drop a collection.
- dbAdmin can read every document in the database it administers.
- clusterAdmin includes read and write access to every application collection.
- clusterAdmin is purely operational and cannot destroy application data.
- Granting readWrite to a user created in admin gives write access to every database.