52

I am using MongoDB Atlas as my database.

I am using angular4 with loopback as api.

My application connects to my database fine. However, when I try to get data, I get this error (I have replaced my dbname with dbname):

MongoError: user is not allowed to do action [find] on [dbname.$cmd]

The query works fine if I use a local mongodb client or a mongodb instance on AWS. However, when using atlas, I get this error.

1
  • 4
    I had this error, my problem was that I had not changed the database name from "test" to my own database when coping the connection string.
    – Meier
    Dec 8, 2017 at 12:43

28 Answers 28

53

In the Atlas cluster, select Security, go to Database Access click on Edit under database user privileges select Built-in role and under the dropdown select Read and write to any database to the user. Try again or refresh your connection to see the result.

2
  • 10
    For me this doesn't work. I have to choose atlasAdmin Nov 10, 2022 at 18:07
  • 1
    For giving access to a specific database, go to 'Specific Privileges' select readWrite from the dropdown, enter database name and live the collection blank. I suggest that do not define a user which can access everything in the system!
    – kaya
    Jan 17, 2023 at 23:19
23

Ugh! This took way too much time to fix! For those of you who have the same issue. I had to do both:

1> I had this error, my problem was that I had not changed the database name from "test" to my own database when coping the connection string. – Meier Dec 8 '17 at 12:43

2>

"mongodb://:@cluster0-shard-00-00- rb899.mongodb.net:27017,cluster0-shard-00-01- rb899.mongodb.net:27017,cluster0-shard-00-02- rb899.mongodb.net:27017/?ssl=true&replicaSet=Cluster0-shard- 0&authSource=admin"

Also, In Atlas, you have to specify the older driver 2.2.12 or later. After doing these things it finally worked.

Thanks all!

15

I had the same error myself.

  • If you are using v.3.0 of the MongoDB NodeJS driver refer to MikaS post for the MongoClient.connect changes that need to be made. https://stackoverflow.com/a/47662979/8775223

  • To connect your application to Atlas MongoDB use the 3.4 driver eg.

    mongodb://<USERNAME>:<PASSWORD>@cluster0-shard-00-00-
    rb899.mongodb.net:27017,cluster0-shard-00-01-
    rb899.mongodb.net:27017,cluster0-shard-00-02-
    rb899.mongodb.net:27017/<DBNAME>?ssl=true&replicaSet=Cluster0-shard-
    0&authSource=admin
    
1
  • 1
    You saved me a lot of time looking for a solution. Thank you very much
    – Gonzalo
    Jan 19, 2018 at 15:18
12

Changed the Role in MongoDB Roles under Database Access to "AtlasAdmin" and it worked.

enter image description here

1
9

In the Atlas cluster, click Database Access under SECURITY on the left sidebar.

Click on the button Edit and then add a role atlasAdmin@admin to the user.

enter image description here enter image description here

8

https://docs.mlab.com/troubleshooting-atlas-connection-issues/#getlog-error

For anyone get the user is not allowed to do action [getLog] on [admin.] error. Had to explicitly give the user the clusterMonitor role

1
  • This answer is helpul! thx.
    – MOLLY
    Feb 10, 2023 at 7:16
5

You can go to Atlas->Security->Database Access tab and add new user with Atlas admin role and then you can connect with the following cmd

mongo "mongodb+srv://cluster0.0q8wi.mongodb.net/admin" --username <username> -p <password>
5

Usually, this error happens because your current user in the database haven't suitable credentials.

  1. Go to the Database Access below the Security section.
  2. Click edit the user which you added. enter image description here
  3. In the Built-in Role section, select Atlas admin) and press Update User button. enter image description here
4

Go to Atlas website

Database Access > Database Users > Edit(your user) > Built-in Role >> change to Atlas admin

3

If you get the following error modifying a view on a table, I found that you needed to give "atlas admin" permission.

user is not allowed to do action [collMod] on [<database>.accounts-list]

The standard readWriteAnyDatabase@admin did not work for me. Give the user "Atlas admin" permission.

1
  • I'm using the cloud.mongodb.com service and changed the permissions via their console.
    – Nick N
    Oct 25, 2021 at 14:09
3

Go to Database Access->Edit->Build-in-Role->Select atlas_admin update user

2

If you are using v3.0 mongodb for Node.js you should use the new API as follows, and the MongoDB Atlas URI connection string for driver 3.4 and earlier:

MongoClient.connect(uri, function(err, client) {
  console.log("Connected successfully to server");

  const db = client.db(dbName);
});
1
  • finally some answer in clear words what needs to be done! I was using the command for shell 3.6 or later
    – Sumit
    Mar 27, 2018 at 7:07
1

Make sure to whitelist the IP you are using to connect from the Mongo CLI to your DB. Do this under the project Network Access tab.

1

If you get the error " user is not allowed to do action [insert]. In your mongoDb account, navigate to Database Access then click on the edit button for the user trying to access, navigate to special privileges and click on "Add special Previledge" select the role option and change the role to "readWriteAnyDatabase"

1

To create or modify a database user, please go to cloud.mongodb.com and go to the Database Access tab and make sure that your user has the readWriteAnyDatabase@admin access so you can read or write the data in your MongoDB database

Check it please https://www.mongodb.com/community/forums/t/mongoservererror-user-is-not-allowed-to-do-action/166721

1

I had the same error when using MongoDB and trying to add data. Finally, I found the solution. The connect string needs to add the project name:

mongodb+srv://:@cluster0.pm9okyd.mongodb.net/?retryWrites=true&w=majority

enter image description here

The project name can be seen in this picture.

3
  • This is what I was looking for 2 nights. Thanks, buddy :)
    – Dženis H.
    Jun 8, 2023 at 20:05
  • How do you add it to your connection string? Your example connection string doesn't seem to contain "test": mongodb+srv://:@cluster0.pm9okyd.mongodb.net/?retryWrites=true&w=majority Jul 4, 2023 at 6:58
  • mongodb+srv://<account>:<password>@cluster0.pm9okyd.mongodb.net/test?retryWrites=true&w=majority
    – yanzzz
    Jul 9, 2023 at 7:27
1

Adding dbAdmin privilege fixed the error for me

enter image description here

0

I got past this by getting the collection object directly and executing find (and other operations) on it:

try {
  mongoose.connect(MONGODB_URI, options)
  const db = mongoose.connection
  db.on("error", console.error.bind(console, "connection error:"))
  db.once("open", async () => {
    console.log("we're connected!")

    const collectionObj = db.collection(ATLAS_COLLECTION)

    const result = await collectionObj.find({}).toArray()
    console.log(result)
}) } catch (error) { console.log({ error }) }
0

I had the same issue and how I fixed it : Make sure you add the correct MongoDB URL : . Go to Cluster - Connect . Choose 'Connect your Application' . Copy the Url (don't forget to fill out your MongoDB password)

0

All you need to do is change the user rol to "atlasAdmin" at MongoDB Atlas (DataBase Access)

0

This also happens if you didn't load credentials correctly, you might have started the script that doesn't load .env variables in case you stored credentials in them (ex. nodemon.json)

0

the issue is that in database access user have some restriction maybe. --------solution: you dont have to create the user in database Access. you can create when you connect to cluster0--------

1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Sep 21, 2022 at 13:14
0

Go to Database access Edit your user and under built in roles select Atlas Admin and save the changes, it should work

0

I solved issue by adding authMechanism: 'SCRAM',

type: 'mongodb',
  url: 'mongodb+srv://<account>:<password>@cluster0.pm9okyd.mongodb.net/<projectname>?retryWrites=true&w=majority',
  authSource: 'admin',
  useNewUrlParser: true,
  synchronize: true,
  logging: true,
  useUnifiedTopology: true,
  entities: ['dist/**/*.entity{.ts,.js}'],
  loggerLevel: 'debug',
  authMechanism: 'SCRAM',
0

To create or modify a database user, please go to cloud.mongodb.com and go to the Database Access tab and make sure that your user has the readWriteAnyDatabase@admin access so you can read or write the data in your MongoDB database with…

1
  • Please take a look at the other answers that were given before. Your approach is mentioned there already. In order to keep the site clear and make it easy to find answers, we try to avoid double answers.
    – ahuemmer
    Dec 21, 2022 at 9:01
0

Go to the Security Section, then Database Access. Edit and select atlasAdmin in the Built-in Role section. Reconnect MongoDB Compass before trying again.

0

TL/DR: If you are creating a custom role:

  • empty collection name means "all". Do not use the '*' wild card

  • check if your database & collection names are correct. Cloud Atlas won't validate the information you provide and maybe the issue is just a typo

Context - 2024 Answer

This response is for those trying to limit their account rights and follow the Least Privilege Access Policy.

Long Story, Short: readWriteAnyDatabase@admin is too powerful and a security breach. Give only the necessary rights if you want to go safe.

How to do it (Atlas/Services/Security):

  1. When creating/updating MongoDB users, you have three types of Database User Privileges:
  • Built-in Role: Atlas Admin, ReadWriteAnyDatabase, ReadAnyDatabase - Easiest Step, but the "any database" should scare us all

  • Custom Role: I recommend this one because it's scalable as you can use roles with other accounts

  • Specific Privilege: it's the same as before, but cannot be replicated to other users

Step by Step

The error we are addressing is: user is not allowed to do action [find] on [a_database.my_collection].

error

We start from Atlas/Services/Security, adding/editing a user:

edit or create a user using Cloud Atlas

Solution: We will create a new role for a database named a_database and we will check the strategies for the Collection Name. Select only the rights the user will need usually: find, insert, remove, update.

least privilege policy

Database Name: A bit confusing, because it's the ones listed under collections in the Cloud Atlas.

database

Custom Role is Ready:

custom role

Important: If you have many collections, leave it empty instead of using the wildcard * in the collection name. If you want to restrict access, you must create multiple actions representing each of the collections.

Some Examples of How to Use this. See that we are referencing two different collections of the same "a_database" and we left b_database empty, to represent all the collections. Be careful with the names there, Atlas won't validate and this might be the cause of your error.

custom roles examples

References: Least Privilege Access Policy Mongo DB Roles

-1

sometimes Api and MongoDB compass, shell (clients) doesn't allow for deleting the database. In that way to delete the database in MongoDB.

  1. Login into MongoDB
  2. Go to the Database menu on the left side of the navigation menu
  3. click on browse collections in Atlas cluster
  4. Delete the database that you want.

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.