title: Error while trying to connect my Express app with MongoDB Atlas
body: <p>I've almost finished my app and I want to connect my MongoDB database to the serverless platform MongoDB Atlas. Until then, I've been using my database in localhost (with mongosh) and it worked perfectly fine. Now I've changed &quot;mongodb://localhost&quot; to the adress of my database and I get an error : <em>MongoNotConnectedError: MongoClient must be connected to perform this operation</em></p>
  <p>Here is my code in db-connection.js :</p>
  <pre><code>require('dotenv').config();
  let { MongoClient } = require('mongodb');
  
  function connect(collection) {
      let client = new MongoClient(`mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASSWORD}@calendrier-delle.2rqpi.mongodb.net/calendrierdelle?retryWrites=true&amp;w=majority`);
      client.connect();
      let calendrierDelle = client.db('calendrierdelle');
      let collections = calendrierDelle.collection(`${collection}`);
  
      return collections;
  }
  
  module.exports = { connect };
  </code></pre>
  <p>And a snippet of my code in server.js :</p>
  <pre><code>require('dotenv').config();
  let express = require(&quot;express&quot;);
  let app = express();
  let dbConnection = require(&quot;./db-connection.js&quot;);
  
  //Here is an example of how I make an query
  let query = dbConnection.connect(&quot;aCollection&quot;).find({
     something: (some value),
  }).toArray();
  query.then((data) =&gt; { /* Do something with the data */ });
  </code></pre>
  <p>To precise :</p>
  <p>• I don't use mongoose because I don't really need, and I'm lazy to change lol.</p>
  <p>• On MongoDB Atlas, I've added an IP adress 0.0.0.0/0</p>
  <p>• My username and password are goods</p>
  <p>• Here is a screenshot of my cluster's name and db's name<a href="https://i.stack.imgur.com/nwdBW.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nwdBW.png" alt="MongoDB Atlas" /></a></p>
  <p>In my code to connect with my database, I don't really understand why at the end of the url I connect my app with the database, and after I have to do the same thing : client.db('calendrierdelle'). I think here is my problem but I don't know how to solve it : I've tried everything (changing url, omit my calendrierDelle variable etc...).</p>
  <p>Can you help me please ? Thanks !</p>
  <p>PS : sorry I've added a mongoose tag to that post, I don't use it, but it's pretty close to my problem.</p>