MongoError: user is not allowed to do action
Asked Answered
S

27

54

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.

Shad answered 9/10, 2017 at 14:55 Comment(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.Sprint
B
55

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.

Brainstorming answered 16/6, 2018 at 12:47 Comment(2)
For me this doesn't work. I have to choose atlasAdminPelagia
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!Cruiser
E
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!

Elodiaelodie answered 7/9, 2019 at 21:43 Comment(0)
H
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://mcmap.net/q/129940/-db-collection-is-not-a-function-when-using-mongoclient-v3-0

  • 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
    
Heisel answered 12/12, 2017 at 14:28 Comment(1)
You saved me a lot of time looking for a solution. Thank you very muchDuplicator
T
13

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

enter image description here

Timothytimour answered 15/11, 2021 at 6:37 Comment(1)
worked indeed !!Consensus
E
10

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

Emirate answered 30/8, 2022 at 20:31 Comment(0)
E
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

Elva answered 14/11, 2020 at 19:49 Comment(1)
This answer is helpul! thx.Zipper
C
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>
Chrisse answered 14/12, 2021 at 2:45 Comment(0)
D
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
Defile answered 13/2, 2023 at 6:17 Comment(0)
N
4

Go to Atlas website

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

Nicknickel answered 14/6, 2022 at 10:42 Comment(0)
H
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.

Hinny answered 25/10, 2021 at 14:8 Comment(1)
I'm using the cloud.mongodb.com service and changed the permissions via their console.Hinny
R
3

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

Roundup answered 9/9, 2022 at 2:55 Comment(0)
N
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);
});
Newcomb answered 6/1, 2018 at 11:30 Comment(1)
finally some answer in clear words what needs to be done! I was using the command for shell 3.6 or laterKaryotype
M
2

Adding dbAdmin privilege fixed the error for me

enter image description here

Munro answered 12/10, 2023 at 4:38 Comment(0)
R
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.

Repudiation answered 18/12, 2020 at 5:16 Comment(0)
Z
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"

Zitvaa answered 27/8, 2022 at 16:25 Comment(0)
Q
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.

Quanta answered 29/8, 2022 at 5:21 Comment(3)
This is what I was looking for 2 nights. Thanks, buddy :)Wagonage
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=majorityRamakrishna
mongodb+srv://<account>:<password>@cluster0.pm9okyd.mongodb.net/test?retryWrites=true&w=majorityQuanta
Y
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

Yearbook answered 30/9, 2022 at 9:7 Comment(0)
V
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 }) }
Vargueno answered 9/4, 2021 at 5:56 Comment(0)
S
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)

Sullyprudhomme answered 10/6, 2021 at 22:37 Comment(0)
W
0

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

Woolgathering answered 6/11, 2021 at 16:43 Comment(0)
C
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)

Coimbatore answered 3/1, 2022 at 12:39 Comment(0)
J
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--------

Jasmine answered 15/9, 2022 at 9:54 Comment(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.Vivisectionist
L
0

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

Lenient answered 20/11, 2022 at 10:25 Comment(0)
B
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',
Bayern answered 24/11, 2022 at 1:27 Comment(0)
S
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…

Stopcock answered 19/12, 2022 at 2:18 Comment(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.Knockknee
G
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

Gargantuan answered 25/3 at 13:14 Comment(0)
E
-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.
Endstopped answered 4/11, 2021 at 13:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.