MongoError: (Unauthorized) not authorized on admin to execute command { find: "sections", filter:


I’m new when it comes to use MongoDB Atlas and have run into this error:

MongoError: (Unauthorized) not authorized on admin to execute command { find: "sections", filter: {  }, projection: {  }, lsid: { id: {4 [23 39 50 0 26 28 73 245 162 202 224 56 75 120 116 112]} }, $clusterTime: { clusterTime: {1676817340 1020}, signature: { hash: {0 [27 218 228 177 190 112 145 244 152 40 238 254 19 76 223 89 148 194 219 33]}, keyId: 7155525947335114752.000000 } }, $db: "admin" }
    at Connection.<anonymous> (/Users/filipwieselgren/Desktop/FED/improveme-bot/node_modules/mongoose/node_modules/mongodb/lib/core/connection/pool.js:453:61)
    at Connection.emit (node:events:513:28)
    at processMessage (/Users/filipwieselgren/Desktop/FED/improveme-bot/node_modules/mongoose/node_modules/mongodb/lib/core/connection/connection.js:456:10)
    at TLSSocket.<anonymous> (/Users/filipwieselgren/Desktop/FED/improveme-bot/node_modules/mongoose/node_modules/mongodb/lib/core/connection/connection.js:625:15)
    at TLSSocket.emit (node:events:513:28)
    at addChunk (node:internal/streams/readable:324:12)
    at readableAddChunk (node:internal/streams/readable:297:9)
    at Readable.push (node:internal/streams/readable:234:10)
    at TLSWrap.onStreamRead (node:internal/stream_base_commons:190:23) {
  ok: 0,
  code: 8000,
  codeName: 'AtlasError'

I have found similar topics here but not something that helped. Anyone that might be able to help?

Kind regards

Hi @Filip_Wieselgren and welcome to the MongoDB community forum!!

There could be multiple reasons if the above error message have been observed. To help you with the correct solution, could you provide the following information regarding the deployment?

  1. The user created for the deployment. You can read about createUser and getUser from the attached documentation.
  2. The roles granted to the user. The documentation on Build-in user roles would help you to understand more on roles and authentication.
  3. The query used on the database which generates the above error and on which database the query is applied .
  4. The connection URI used to connect to the Atlas cluster.

Please note that, if you are using the above query on the admin database, this activity is restricted from the MongoDB server, as the Admin, local, config are used for the internals of MongoDB.

Also, for shared tiers(M0/M2/M5), there might be some operation limitations which you can refer for further understanding.

Let us know if you have any further questions.

Best Regards

1 Like


MongoError: (Unauthorized) not authorized on admin to execute command { find: "sections", filter: { }, ..., $db: "admin" }

The issue appears to be that you’re trying to query a namespace you don’t have access to. If you connected to your cluster using a connection string such as mongodb+srv://<user>:<pass> the database the MongoClient will try to use will be the admin database (by default when authentication is used).

You can see this in the error you provided ($db: "admin"), so the easiest way to address this is to provide a database in the connection string (mongodb+srv://<user>:<pass>

By adding a database name to the connection string the query you were attempting will target myDatabase.sections, which your user should have full access to as opposed to admin.sections, which would be restricted (as it is in the admin database).

The Mongoose documentation alludes to this as well.

As you’re new to MongoDB and MongoDB Atlas it’s worth noting there are Unsupported Commands in Atlas (such as createUser) that may be available in standalone deployments.

1 Like