<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Vero</title>
    <description>The latest articles on DEV Community by Vero (@veroca88).</description>
    <link>https://dev.to/veroca88</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F587115%2Fbf055cbe-3715-4648-9220-4616430def40.jpeg</url>
      <title>DEV Community: Vero</title>
      <link>https://dev.to/veroca88</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/veroca88"/>
    <language>en</language>
    <item>
      <title>Set up MongoDB Atlas and connect with Node.js (avoid MongooseError: Operation x.findOne() buffering timed out after 10000ms)</title>
      <dc:creator>Vero</dc:creator>
      <pubDate>Sat, 17 Apr 2021 20:21:26 +0000</pubDate>
      <link>https://dev.to/veroca88/set-up-mongodb-atlas-and-connect-with-node-js-avoid-mongooseerror-operation-x-findone-buffering-timed-out-after-10000ms-5dpi</link>
      <guid>https://dev.to/veroca88/set-up-mongodb-atlas-and-connect-with-node-js-avoid-mongooseerror-operation-x-findone-buffering-timed-out-after-10000ms-5dpi</guid>
      <description>&lt;h1&gt;
  
  
  Setup MongoDB Atlas
&lt;/h1&gt;

&lt;ol&gt;
&lt;li&gt;Create your account: 
You can sign up with Google or register with your email.&lt;/li&gt;
&lt;li&gt;Accept Privacy Policy &amp;amp; Terms of Service.&lt;/li&gt;
&lt;li&gt;Name your organization and project.&lt;/li&gt;
&lt;li&gt;You can select your preferred language (in our case JavaScript) you can select only one but you can always change this later.&lt;/li&gt;
&lt;li&gt;Create a cluster - FREE -.&lt;/li&gt;
&lt;li&gt;Select your preferred Cloud Provider and region.&lt;/li&gt;
&lt;li&gt;The additional settings leave like that because there are not included in the Cluster Tier.&lt;/li&gt;
&lt;li&gt;Name your Cluster. &lt;/li&gt;
&lt;li&gt;Click Create Cluster.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now look at the name of your organization, is located at the top left of your screen.&lt;br&gt;
Below is the name of the current project, If you click in the name you are going to see.&lt;br&gt;
View all projects - New Project&lt;/p&gt;

&lt;p&gt;You can create a new cluster o work with the one in use.&lt;/p&gt;
&lt;h1&gt;
  
  
  Connect to Cluster
&lt;/h1&gt;

&lt;p&gt;🔥 here is the first critical step 🔥&lt;br&gt;
To make sure your cluster accept the incoming data is important to tell it which channel is safe to received the data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Setup connection security&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;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 &lt;strong&gt;my ip addres&lt;/strong&gt; and it'll show your public IP address.
Description is optional but I described it as "Home".&lt;/li&gt;
&lt;li&gt;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.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;When you finish the setup connection security &lt;br&gt;
Click Choose a connection method&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Choose a connection method&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In this example we are going to select &lt;strong&gt;Connect your application&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;As we are working with Node.js the drives must be Node.js and the version you can choose 3.6 or later.&lt;/p&gt;

&lt;p&gt;(I saw in multiple blogs to solve this issue &lt;br&gt;
&lt;code&gt;MongooseError: Operation x.findOne() buffering timed out after 10000ms&lt;/code&gt; some people suggested to lower the version of Node.js but I found it doesn't solve the issue)&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Copy the connection string 🔍 (not the full driver code example) and save it to paste it in the .env file in your application later.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Almost!!!&lt;/strong&gt; ⏰&lt;/p&gt;

&lt;p&gt;Ok! we are done with the MongoDB Atlas, now we move to our application.&lt;/p&gt;
&lt;h2&gt;
  
  
  Configuration in the application
&lt;/h2&gt;

&lt;p&gt;🔥 here is the second critical step 🔥&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;We need to install all the packages we are going to need.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;npm install mongoose dotenv&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;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. &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Remember replace username and password with those who created previously.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;MONGODB_URI=mongodb+srv://&lt;strong&gt;username&lt;/strong&gt;:&lt;strong&gt;password&lt;/strong&gt;@clusterName.mongodb.net/myFirstDatabase?retryWrites=true&amp;amp;w=majority&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Finally, in your app.js you can add the code below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// 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 =&amp;gt; {
    console.log(
      `Connected to Mongo! Database name: "${x.connections[0].name}"`
    );
  })
  .catch(err =&amp;gt; {
    console.error("Error connecting to mongo", err);
  });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>mongodb</category>
      <category>node</category>
      <category>javascript</category>
      <category>database</category>
    </item>
  </channel>
</rss>
