title: MongoDB custom user roles - "user is not allowed(...)"
body: <p>I created a free tier cluster on MongoDB Atlas (it has 3 shards) and I want my Node.js app to connect with a database I created there, using a specific user, that will be restricted from using any other database than the one inteded for this app.</p>
  
  <p>So step by step.</p>
  
  <p>I create a database called, let's say, <code>test</code>.</p>
  
  <p>I create a role <a href="https://imgur.com/UxASvco" rel="nofollow noreferrer">here</a> - I go to <code>Security -&gt; MongoDB Roles -&gt; Add New Custom Role</code> and I give it all <code>Collection actions</code> and all <code>Database actions and roles</code> to <code>test</code></p>
  
  <p>Time for a user, so again <code>Security -&gt; MongoDB Users -&gt; Add New User</code> and I assign a previously created role to it so it has access only to <code>test</code> database. So now I have 2 users - atlasAdmin and my created user.</p>
  
  <p>That's where the problem occurs, when I use admin user in my app to connect, <code>.find()</code> or <code>.create()</code> it works fine all the time. With a user with custom role, it works for like 10mins/1 connection (until I shut down the local server I have my node app on) and the next time I get an error that "user is not allowed to perform action (...)". </p>
  
  <p>I tried everything, tinkering with a string I use to connect, updating <code>mongoose</code> (I use it in my app), creating user and custom role using mongodb shell but nothing seems to work.</p>
  
  <p>HOWEVER:</p>
  
  <ul>
  <li><p>if I have this custom user, my app connects with it to the database and it works, then on the next connection it stops working AND I go <a href="https://imgur.com/a/kx0sdLi" rel="nofollow noreferrer">here</a> and just click <code>UPDATE USER</code> without changing anything there (I just click <code>edit</code> next to the user and then <code>update</code>) then wait for the cluster to make changes, it will work again for like +/- one connection.</p></li>
  <li><p>everything works just fine if my app uses admin account</p></li>
  </ul>
  
  <p>Anyone had similar problem? <a href="https://imgur.com/a/rdtACQA" rel="nofollow noreferrer">Screenshot of the error</a> I was also thinking that it might be because of how many times I try to connect with mongo from the app (I use <code>nodemon</code> so everytime I save a file with changes, server restarts, thus connecting to database again) but I think that's not the case - if it was, why would I be able to make it work with admin user?</p>
  
  <p>The string I use to connect with mongo:</p>
  
  <pre class="lang-js prettyprint-override"><code>// DATABASE SETUP
  var dbURL = 'mongodb://[cluster0:port],[cluster1:port],[cluster2:port]/test?ssl=true&amp;replicaSet=Cluster0-shard-0&amp;authSource=admin&amp;retryWrites=true';
  var options = {
      useNewUrlParser: true,
      dbName: "test"
      user: [login],
      pass: [pass]
  };
  mongoose.connect(dbURL, options);
  </code></pre>