title: MongooseServerSelectionError: read ECONNRESET
body: <p>I'm trying to connect my Nodejs application to MongoDB Atlas but it's throwing an error. </p>
  
  <p><strong>Here is the error message:</strong></p>
  
  <blockquote>
    <p>This is the error in the catch block: MongooseServerSelectionError:
    read ECONNRESET DB connection error: read ECONNRESET</p>
  </blockquote>
  
  <p><strong>Below is the code for my Nodejs application:</strong></p>
  
  <pre><code>const mongoose = require("mongoose");
  const dotenv = require("dotenv");
  dotenv.config();
  
  //db connection
  mongoose
    .connect(process.env.MONGO_URI, {
      useNewUrlParser: true,
      useUnifiedTopology: true
    })
    .then(() =&gt; console.log("DB Connected"))
    .catch(err =&gt; {
      console.log("This is the error in the catch block: " + err);
    });
  
  mongoose.connection.on("error", err =&gt; {
    console.log(`DB connection error: ${err.message}`);
  });
  </code></pre>
  
  <p><strong>NOTE:-</strong> I declared .env file in which I have a variable MONGO_URI to hold the secret key required for connecting to MongoDB Atlas.</p>