Set up MongoDB Atlas and connect with Node.js (avoid MongooseError: Operation x.findOne() buffering timed out after 10000ms)
Tags: mongodb, node, javascript, database
Published: 2021-04-17 20:21:26

# Setup MongoDB Atlas
1. Create your account: 
You can sign up with Google or register with your email.
2. Accept Privacy Policy & Terms of Service.
3. Name your organization and project.
4. You can select your preferred language (in our case JavaScript) you can select only one but you can always change this later.
5. Create a cluster - FREE -.
6. Select your preferred Cloud Provider and region.
7. The additional settings leave like that because there are not included in the Cluster Tier.
8. Name your Cluster. 
9. 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**

1. 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".
2. 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)

3. 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 🔥

1. We need to install all the packages we are going to need.
> npm install mongoose dotenv

2. 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);
  });
```