Setup MongoDB Atlas
- Create your account: You can sign up with Google or register with your email.
- Accept Privacy Policy & Terms of Service.
- Name your organization and project.
- You can select your preferred language (in our case JavaScript) you can select only one but you can always change this later.
- Create a cluster - FREE -.
- Select your preferred Cloud Provider and region.
- The additional settings leave like that because there are not included in the Cluster Tier.
- Name your Cluster.
- Click Create Cluster.
Now look at the name of your organization, is located at the top left of your screen.
Below is the name of the current project, If you click in the name you are going to see.
View all projects - New Project
You can create a new cluster o work with the one in use.
Connect to Cluster
π₯ here is the first critical step π₯
To make sure your cluster accept the incoming data is important to tell it which channel is safe to received the data.
Setup connection security
- Add a connection IP address: As usually MongoDB knows your IP address but if you want to be sure you can go to your favorite search engine and type my ip addres and it'll show your public IP address. Description is optional but I described it as "Home".
- Create Database User: Keep your credentials handy, you'll need them for the configuration in your application. You have to set a user name and password.
When you finish the setup connection security
Click Choose a connection method
Choose a connection method
In this example we are going to select Connect your application.
As we are working with Node.js the drives must be Node.js and the version you can choose 3.6 or later.
(I saw in multiple blogs to solve this issue
MongooseError: Operation x.findOne() buffering timed out after 10000ms
some people suggested to lower the version of Node.js but I found it doesn't solve the issue)
- Copy the connection string π (not the full driver code example) and save it to paste it in the .env file in your application later.
Almost!!! β°
Ok! we are done with the MongoDB Atlas, now we move to our application.
Configuration in the application
π₯ here is the second critical step π₯
-
We need to install all the packages we are going to need.
npm install mongoose dotenv
Create .env file at the same level of your app.js and you are going to paste the connection string π in the .env file as show below.
Remember replace username and password with those who created previously.
MONGODB_URI is just a placeholder you can name it as you want but remember if you change the name in .env file you must change the name in app.js.
MONGODB_URI=mongodb+srv://username:password@clusterName.mongodb.net/myFirstDatabase?retryWrites=true&w=majority
Finally, in your app.js you can add the code below.
// Connected to Cluster Atlas MongoDB
require("dotenv").config();
const mongoose = require('mongoose');
const uri = process.env.MONGODB_URI
mongoose
.connect(uri, {
useCreateIndex: true,
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false
})
.then(x => {
console.log(
`Connected to Mongo! Database name: "${x.connections[0].name}"`
);
})
.catch(err => {
console.error("Error connecting to mongo", err);
});
Top comments (2)
I tried this, it didn't work. I keep getting the x.insertOne() error
It might be because you are trying to use models before mongoose establishes a connection with the database. I too faced the same issue and was able to solve it by using async/await. You could check the details out in the following article. Hope this was helpful βοΈ
Solve "MongooseError: Operation 'x.find()` buffering timed out after 10000ms"
arunkc γ» May 16 γ» 3 min read