2 / 9
Oct 2021

I have created free cluster and db on Mongo Atlas but I try to connect my app by :slight_smile: mongoose.connect(‘mongodb+srv://Ahmed:Admin123@st.0qt0i.mongodb.net/bktrans?retryWrites=true&w=majority’)

And the below error appears when I run rpm start on my API:

(Unauthorized)not authorized on admin to execute command {listIndexes: “consumers“, cursor:{ }, $clusterTime: { clusterTime: {1634734692 10}, signature: { hash: {203 165 207 4 203 170 4 127 37 213 33 4 100 167 170 44 201 49 111 36} }} } …

Hi Ahmed ,

I got this error recently while connecting from a node app on Heroku to MongoDB Atlas - I had issue with connection string .

I tried using below the below format - and it worked.

mongodb://:@-cluster0-shard-00-00.scxjr.mongodb.net:27017,-cluster0-shard-00-01.scxjr.mongodb.net:27017,-cluster0-shard-00-02.scxjr.mongodb.net:27017/?ssl=true&replicaSet=atlas--shard-0&authSource=admin&retryWrites=true&w=majority

It worked.

I got this from MongoDB Atlas tab → Deployment side bar → Databases → on right Connect button inside . Connect to your application section → Select Driver - Node.js and Version 2.2.12 or later - below it will show the example connection string.

Please let me know if this solved your issue

6 months later
11 months later

hello, I am getting same kind of error like:
(Unauthorized) not authorized on admin to execute command { create: “sample”, capped: true, size: 5242880, writeConcern: { w: “majority” }, lsid: { id: {4 [133 92 127 66 175 140 79 35 174 118 57 40 59 137 39 110]} }, $clusterTime: { clusterTime: {1678037479 5}, signature: { hash: {0 [235 83 26 247 241 208 26 217 203 154 205 184 108 184 228 33 93 229 204 81]}, keyId: 7153350679543676928.000000 } }, $db: “admin” }

i tried multi[ple ways for creating collection in db. This error i got is when i got connected to mongodb compass and tried to build a connection in db.
There are 3 radio buttons available with that, but cant choose which one to choose.
Please help me out.

I think you are trying to create your collection in admin db
Connect to test db and try
Also share your compass connect string after hiding sensitive information like password,cluster address

8 months later

hi
I use your method to get connect string. But what it gives me is mongodb://zmz2:123@ac-64yh6ax-shard-00-00.pbddalw.mongodb.net:27017

5 months later
5 months later
7 months later

As already noted here you can avoid this by connecting to a database other than the admin one.

To create a new database if none exist, follow this handy guide.Note the database name.

Now, back to your connection uri string, specify that you want to connect to this database specifically. The string should look something like this:

mongodb+srv://<user>:<db-password>@<cluster-name>.d2ylz.mongodb.net/<db-name>

Substitute db-name`` for your actual database name. (You should also already have substituted for user and database password etc.)

This was tested with mongoose > 7.0