Mongoerror: User Is Not Allowed to Do Action
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...
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.
26 Answers
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.
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!
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.
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
Changed the Role in MongoDB Roles under Database Access to "AtlasAdmin" and it worked.
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.
For anyone get the user is not allowed to do action [getLog] on [admin.] error. Had to explicitly give the user the clusterMonitor role
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://" --username <username> -p <password>
Go to Atlas website
Database Access > Database Users > Edit(your user) > Built-in Role >> change to Atlas admin
Usually, this error happens because your current user in the database haven't suitable credentials.
- Go to the
Database Accessbelow theSecuritysection. - Click
editthe user which you added. - In the
Built-in Rolesection, selectAtlas admin) and pressUpdate Userbutton.
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.
Go to Database Access->Edit->Build-in-Role->Select atlas_admin update user
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);
});
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.
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"
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
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://:@
The project name can be seen in this picture.
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 }) }
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)
All you need to do is change the user rol to "atlasAdmin" at MongoDB Atlas (DataBase Access)
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)
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--------
Go to Database access Edit your user and under built in roles select Atlas Admin and save the changes, it should work
I solved issue by adding authMechanism: 'SCRAM',
type: 'mongodb',
url: 'mongodb+srv://<account>:<password>@
authSource: 'admin',
useNewUrlParser: true,
synchronize: true,
logging: true,
useUnifiedTopology: true,
entities: ['dist/**/*.entity{.ts,.js}'],
loggerLevel: 'debug',
authMechanism: 'SCRAM',
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…
Adding dbAdmin privilege fixed the error for me
sometimes Api and MongoDB compass, shell (clients) doesn't allow for deleting the database. In that way to delete the database in MongoDB.
- Login into MongoDB
- Go to the Database menu on the left side of the navigation menu
- click on browse collections in Atlas cluster
- Delete the database that you want.