title: Error: MongoError: not authorized on admin to execute command
body: <p>So, I am following along with Beau in <a href="https://www.youtube.com/watch?v=7CqJlxBYj-M&amp;" rel="nofollow noreferrer">this</a> tutorial over on the freeCodeCamp YouTube learning to build a simple app using the MERN Stack. While using Postman however, I am getting this error message when trying to send a POST request to <code>localhost:5000/users/add</code>:</p>
  
  <pre><code>"Error: MongoError: not authorized on admin to execute command { insert: \"users\", documents: [[{_id ObjectIdHex(\"5d96cd3f31092833b8253260\")} {username Andrew} {createdAt 2019-10-04 04:40:31.321 +0000 UTC} {updatedAt 2019-10-04 04:40:31.321 +0000 UTC} {__v 0}]], ordered: true, writeConcern: { w: \"majority\" }, lsid: { id: {4 [40 157 203 39 59 227 66 72 188 54 104 29 179 241 37 148]} }, txnNumber: 2.000000, $clusterTime: { clusterTime: 6743803110960922625, signature: { hash: [61 192 72 112 135 6 249 47 34 239 28 238 196 104 30 46 4 217 216 107], keyId: 6741907548619669504.000000 } }, $db: \"admin\" }"
  </code></pre>
  
  <p>I have spent the last couple of hours looking online and at multiple SO Q+A threads but I cannot seem to find a solution to this problem. I see that a common suggestion is to give my user root access in MongoDB Atlas, but I am unsure where I would implement this. I am also using all of the free options, and I have read that since that I am using those options, this error cannot be bypassed?</p>
  
  <p>Here is the current access for the user I created for this project:
  <a href="https://i.stack.imgur.com/D9fQZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/D9fQZ.png" alt="enter image description here"></a></p>
  
  <p>And under Network Access I have selected my own IP Address (using any address did not help).</p>
  
  <p>Here is the code for our <strong>server.js</strong> file:</p>
  
  <pre><code>const express = require('express');
  const cors = require('cors');
  const mongoose = require('mongoose');
  
  require('dotenv').config();
  
  const app = express();
  const port = process.env.PORT || 5000;
  
  app.use(cors());
  app.use(express.json());
  
  const uri = "mongodb+srv://&lt;username&gt;:&lt;password&gt;@cluster0-idjjd.gcp.mongodb.net/admin?retryWrites=true&amp;w=majority"; 
  mongoose.connect(uri, { useUnifiedTopology: true, useNewUrlParser: true, useCreateIndex: true });
  const connection = mongoose.connection;
  connection.once('open', () =&gt; {
    console.log('MongoDB database connection established successfully!');
  });
  
  const exercisesRouter = require('./routes/exercises');
  const usersRouter = require('./routes/users');
  
  app.use('/exercises', exercisesRouter);
  app.use('/users', usersRouter);
  
  app.listen(port, () =&gt; {
    console.log(`The server is running on port ${port}`);
  });
  </code></pre>
  
  <p>I have of course changed the username and password for my own in the <code>uri</code> variable, just didn't want to enter it here. </p>
  
  <p>And when I run <code>nodemon server.js</code>, I see in the terminal:</p>
  
  <pre><code>[nodemon] restarting due to changes...
  [nodemon] starting node server.js
  The server is running on port 5000
  MongoDB database connection established successfully!
  </code></pre>
  
  <p>Here is the code for our <strong>user.model.js</strong> file:</p>
  
  <pre><code>const mongoose = require('mongoose');
  
  const Schema = mongoose.Schema;
  
  const userSchema = new Schema ({
    username: {
      type: String,     
      required: true,   
      unique: true,     
      trim: true,       
      minlength: 3      
    },
  }, {
    timestamps: true
  })
  
  const User = mongoose.model('User', userSchema);
  
  module.exports = User;
  </code></pre>
  
  <p>And here is the code for our <strong>users.js</strong> file used for routing:</p>
  
  <pre><code>const router = require('express').Router();
  let User = require('../models/user.model');
  
  router.route('/').get((request, response) =&gt; {
    User.find() 
      .then(users =&gt; response.json(users))
      .catch(error =&gt; response.status(400).json(`Error: ${error}`))
  });
  
  router.route('/add').post((request, response) =&gt; {
    const username = request.body.username;
    const newUser = new User({ username });
  
    newUser.save()
      .then(() =&gt; response.json('User added!'))
      .catch(error =&gt; response.status(400).json(`Error: ${error}`))
  });
  
  module.exports = router;
  </code></pre>
  
  <p>I have followed the tutorial and even compared my code to the code to the completed repo, and I cannot make out any difference, so I am unsure how to to fix this error.</p>
  
  <p>Again in Postman I am selecting <code>POST</code>, then entering <code>localhost:5000/users/add</code>, and selecting <code>Body</code>, then choosing <code>raw</code> and <code>JSON</code>. I am entering the following:</p>
  
  <pre><code>{
      "username": "Andrew"
  }
  </code></pre>
  
  <p>If anyone could help me solve this issue it would be much appreciated. Again, this is my first experience using the MERN stack and I really like the idea of using MongoDB alongside Express/React, so I really want to solve this not only for this project, but as well any future projects. </p>
  
  <p>I have also read that examples like mine are lacking the use of bodyParser; however, as mentioned in the video, that is no longer needed and instead we can simply use <code>express</code>, as seen in <code>app.use(express.json());</code>. Is this correct?</p>
  
  <p>Thank you.</p>