I want to setup a user in MongoDB. This user will not have admin database access. But it uses admin as authentication database. It will fail to connect the mongoDB by this command mongo --host localhost admin. Instead, it can use this command to connect to test database: mongo --host localhost --authenticationDatabases admin test. How can I restrict the permission in this case?

I tried below command to create an user:

db.createUser({user: 'testUser', pwd: '123456', roles: [{role:'readWrite', db: 'SampleCollections'}]})

when I use that user account to login mongo shell, I am able to list the collections under admin database. How can I restrict the user only on SampleCollections database not admin?

2 Answers 2


Not need. You just give user needed rights to the wanted non-admin database. User can use admin database as authentication database even user doesn't have read/write access to admin database.

use products
db.grantRolesToUser("productsUser",[ "readWrite" ])

grantRolesToUser documentation.


Let's create mongodb instance with --auth. Login as admin, create user what can readWrite ONLY test -db, authenticate with that user, check can use list admin database collections and what this user can do with test database.

#> mlaunch init --single --auth
launching: mongod on port 27017
Username "user", password "password"
#> mongo -u user -p password admin
MongoDB shell version v3.4.6
connecting to: mongodb://
MongoDB server version: 3.4.6
Mongo> db.createUser({user:"test", pwd:"testpwd", roles:[{role:"readWrite", db:"test"}]})
Successfully added user: {
    "user" : "test",
    "roles" : [
            "role" : "readWrite",
            "db" : "test"
Mongo> db.auth("test","testpwd")
Mongo> db
Mongo> show collections
2017-09-11T19:06:22.001+0300 E QUERY    [thread1] Error: listCollections failed: {
    "ok" : 0,
    "errmsg" : "not authorized on admin to execute command { listCollections: 1.0, filter: {} }",
    "code" : 13,
    "codeName" : "Unauthorized"
} :
Mongo> use test
switched to db test
Mongo> db.coll.insert({})
WriteResult({ "nInserted" : 1 })
Mongo> show collections

And same from outside:

#> mongo -u test -p testpwd --authenticationDatabase test
MongoDB shell version v3.4.6
connecting to: mongodb://
MongoDB server version: 3.4.6
2017-09-11T19:17:48.614+0300 E QUERY    [thread1] Error: Authentication failed. :
exception: login failed
#> mongo -u test -p testpwd --authenticationDatabase admin test
MongoDB shell version v3.4.6
connecting to: mongodb://
MongoDB server version: 3.4.6
  • I tried to create an user with this role but the user can view the admin database. Please see the command I used on my post. Sep 10, 2017 at 12:28
  • Remember to change that DB first, with that use xxxx, because that command gives rights to currently selected DB.
    – JJussi
    Sep 10, 2017 at 17:12
  • I have tried that. In this case, I need to use the xxxx as the authentication database. But what I want is to use admin as the authentication database. Sep 11, 2017 at 12:22
  • @ZhaoYi check my edits at my answer.. There is step by step example. It works!
    – JJussi
    Sep 11, 2017 at 16:13

@Zhao Yi,what you are looking ? I hope so that you shall find out your solution Here

For example i am attaching a one example as shown below

enter image description here

The Explanation of the code is :

  1. The first step is to specify the username and password which needs to be created.
  2. The second step is to assign a role for the user which in this case since it needs to be a database administrator is assigned to the userAdmin role. This role allows the user to have administrative privileges only to the database specified in the db option.
  3. The db parameter specifies the database to which the user should have administrative privileges on.

Note: The output shows that a user called Employeeadmin was created and that user has privileges only on the Employee database.

  • Thanks for your reply. I can create a user with the role based on your command. But that user has access to admin database. I am able to use admin and run show collections. What I need is to use admin as an authentication database but restrict users only on Employee database. So I want this command for user to login: mongo --username Employeeadmin --password 123456 --authenticationDatabase admin SampleCollections Sep 10, 2017 at 23:27
  • @Zhao Yi, As per mongodb documentation docs.mongodb.com/manual/tutorial/manage-users-and-roles Except for roles created in the admin database, a role can only include privileges that apply to its database and can only inherit from other roles in its database. Sep 11, 2017 at 12:54

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.