Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoError: cannot do queries on admin in atlas

I just set up a free MongoDB on Atlas. I added a database called 'react-project' that I'm trying to connect to using mongoose. Here is my connection string:

mongoose.connect('mongodb+srv://myUser:[email protected]/react-project');

As you can see, at the end of the query I specify the database I want to connect to. I can connect to the mongodb instance just fine, but when I try and do an operation (such as Model.find()), I get the following MongoError:

MongoError: cannot do raw queries on admin in atlas

This leads me to believe that it is not connecting to the 'react-project' database, but is connecting to the 'admin' database instead.

Why is this happening and how can I connect to the correct database?

Here is the code I'm running that is giving me the error:

var productSchema = mongoose.Schema({
    productId: Number,
    img: String,
    price: Number,
    title: String,
    type: String
});

var Product = mongoose.model('Product', productSchema);

// Return all products
Product.find(function(error,result) {
    if (error) return console.error(error);
    console.log(result);
});
like image 836
rockzombie2 Avatar asked Feb 28 '18 06:02

rockzombie2


People also ask

Why is MongoDB not connecting to Atlas?

If you have created a user and are having trouble authenticating, try the following: Check that you are using the correct username and password for your database user, and that you are connecting to the correct database deployment. Check that you are specifying the correct authSource database in your connection string.

Can Atlas work with any database?

No. MongoDB Atlas is a database as a service where we run the software on your behalf.

Can I use MongoDB Atlas in production?

You can create project-level Atlas users and roles with appropriate access to the different production and development application environments. Users with the Project Read Only role can access project-level monitoring and system health metadata without having access to any collection data or administrative operations.


4 Answers

I had the exact same issue. Seems like its a new one.

Heres my connection string that now works

mongoose.connect('mongodb://USERNAME:PASSWORD@myShard-shard-00-00-lbofd.mongodb.net:27017,myShard-shard-00-01-lbofd.mongodb.net:27017,myShard-shard-00-02-lbofd.mongodb.net:27017/MYDBNAME?ssl=true&replicaSet=myShard-shard-0&authSource=admin');

To create a new DB I used MongoDB Compass. Copy the connection string from the Atlas site(the longer one - 3.4 I believe), when you open mongodb compass it will recognize that there is a connection string in your clipboard (OSX) and will allow you to populate the connection for Compass with a simple click. You can create a new Database from there (Click on MyCluster at top left - then Create Database. Put the name of the new db in the bolded MYDBNAME in my connection string.

It did take a few refreshes to see new data.

I had much the same connection string as you (db name/shard different etc) - I was able to POST but not GET. POSTS didnt create error (but I could not find data I posted) and GET threw the same error as you got.

like image 141
Yesthe Cia Avatar answered Oct 21 '22 09:10

Yesthe Cia


Had the same issue, none of the above helped. Documenting my solution since this page is the first hit on Google.

If you're using the connect-mongo library like I was, you can fix it by using the older connection string (Node 2.2 not Node 3.0). To get that in Atlas, Click on your cluster -> Connect -> Connect Your Application -> under driver choose node v. 2.2 and copy that connection string.

This is because connect-mongo can't handle the "+srv" in the newer strings.

So that string should look like

mongodb://<user>:<password>@cluster0-shard-00-00-rnmsm.mongodb.net:27017,cluster0-shard-00-01-rnmsm.mongodb.net:27017,cluster0-shard-00-02-rnmsm.mongodb.net:27017/test?ssl=true&replicaSet=cluster0-shard-0&authSource=admin&retryWrites=true&w=majority
like image 38
Doa Avatar answered Oct 21 '22 09:10

Doa


I just got this error after trying to switch from my paid Mongo Atlas cluster back to my free cluster. The solution for me ended up being blissfully simple:

In the URI, I changed /admin to the name of the free cluster's db or /test in my case.

String before fix:

"mongoURI": "mongodb+srv://<username>:<password>@cluster0-azhuz.mongodb.net/admin?retryWrites=true&w=majority"

String after fix:

"mongoURI": "mongodb+srv://<username>:<password>@cluster0-azhuz.mongodb.net/test?retryWrites=true&w=majority"
like image 11
sandtrout Avatar answered Oct 21 '22 09:10

sandtrout


Got the same issue.
Changing connection string to 3.4 version (the longer one instead of 3.6) helped.
Strange, it had worked well on it on 3.6 before the issue appeared.

like image 5
Stan Avatar answered Oct 21 '22 09:10

Stan