29

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.

Janpan
  • 1,876
  • 1
  • 24
  • 49

14 Answers14

17

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
  • 171
  • 1
  • 2
14

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
  • 149
  • 1
  • 6
10

In the Atlas cluster, select Security tab, press on button Edit then add a role readWriteAnyDatabase to the user. Try again or refresh your connection to see the result.

aviit
  • 1,382
  • 1
  • 23
  • 47
5

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
  • 714
  • 1
  • 9
  • 19
2

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

enter image description here

Nandan Wewhare
  • 189
  • 1
  • 10
1

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,455
  • 2
  • 18
  • 22
  • 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
1

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
  • 201
  • 3
  • 4
0

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
  • 49
  • 4
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
  • 26,923
  • 21
  • 73
  • 125
am1991
  • 104
  • 1
  • 7
0

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
  • 924
  • 9
  • 20
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
  • 428
  • 4
  • 6
-1

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