50

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.

Laurel
  • 5,965
  • 14
  • 31
  • 57
Janpan
  • 2,164
  • 3
  • 27
  • 53
  • 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 08 '17 at 12:43

25 Answers25

52

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.

Community
  • 1
  • 1
aviit
  • 1,957
  • 1
  • 27
  • 50
  • 10
    For me this doesn't work. I have to choose `atlasAdmin` – Cao Shouguang Nov 10 '22 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 '23 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!

Michael B
  • 231
  • 2
  • 2
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
    
kostjakon
  • 159
  • 1
  • 6
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

FugueWeb
  • 744
  • 1
  • 9
  • 19
8

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

enter image description here

Nandan Wewhare
  • 445
  • 5
  • 11
8

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

Nditah
  • 1,429
  • 19
  • 23
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>
user2739602
  • 301
  • 5
  • 4
3

Go to Atlas website

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

realPro
  • 1,713
  • 3
  • 22
  • 34
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);
});
itacode
  • 3,721
  • 3
  • 21
  • 23
  • 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 '18 at 07:07
2

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.

Nick N
  • 984
  • 9
  • 22
2

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

2

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
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.

Shaun
  • 79
  • 5
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

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.

Dženis H.
  • 7,284
  • 3
  • 25
  • 44
yanzzz
  • 86
  • 5
  • This is what I was looking for 2 nights. Thanks, buddy :) – Dženis H. Jun 08 '23 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 – ZitRo Jul 04 '23 at 06:58
  • mongodb+srv://:@cluster0.pm9okyd.mongodb.net/test?retryWrites=true&w=majority – yanzzz Jul 09 '23 at 07:27
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

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 }) }
Dharman
  • 30,962
  • 25
  • 85
  • 135
am1991
  • 134
  • 1
  • 7
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)

puny
  • 11
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)

semperlabs
  • 458
  • 4
  • 7
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--------

  • 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](/help/how-to-answer). – Community Sep 21 '22 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

Ayush Bali
  • 21
  • 2
  • 7
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',
Mark O
  • 927
  • 9
  • 13
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…

  • 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 '22 at 09:01
-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.