title: How to solve 'command find requires authentication' using Node.js and mongoose?
body: <p>I need help to fix the error. My ubuntu server 16.04 running:</p>
  
  <p>MongoDB shell version v4.0.9</p>
  
  <p>Node 11.6</p>
  
  <p>mongoose 5.3.4</p>
  
  <p>When the security on /etc/mongod.conf is:</p>
  
  <p><code>security:
          authorization: "disabled"</code>
  I can use :</p>
  
  <pre><code>const mongoURI = 'mongodb://writeApp:writeApp9779@127.0.0.1:27017/writeapp';
  </code></pre>
  
  <p><code>const db = mongoose.connect(mongoURI, { useNewUrlParser: true });</code></p>
  
  <p>All database commands work. Even I checked by putting wrong password, It shows auth fail. But as you know mongodb will still work as authorization is disabled.</p>
  
  <p>My user data is correct as checked by db.getUsers() from command line:</p>
  
  <pre><code>&gt; db.getUsers();
  [
   {
      "_id" : "admin.root",
      "userId" : UUID("8f6c1295-a261-4057-9a29-8a9919437841"),
      "user" : "root",
      "db" : "admin",
      "roles" : [
          {
              "role" : "userAdminAnyDatabase",
              "db" : "admin"
          }
      ],
      "mechanisms" : [
          "SCRAM-SHA-1",
          "SCRAM-SHA-256"
      ]
   },
   {
      "_id" : "admin.writeApp",
      "userId" : UUID("ee148506-1860-4739-80db-17352e0e2ccb"),
      "user" : "writeApp",
      "db" : "admin",
      "roles" : [
          {
              "role" : "dbOwner",
              "db" : "writeapp"
          },
          {
              "role" : "listDatabases",
              "db" : "admin"
          }
      ],
      "mechanisms" : [
          "SCRAM-SHA-1",
          "SCRAM-SHA-256"
      ]
    }
  ]
  </code></pre>
  
  <p><strong>The real problem:</strong>
  so I nano /etc/mongod.conf and changed the authorization:"enabled"
  after that restarted mongo. When I re run my app, following error occurs:</p>
  
  <pre><code>{ MongoError: command find requires authentication
   ............
   ............
   ok: 0,
   errmsg: 'command find requires authentication',
  code: 13,
  codeName: 'Unauthorized',
  name: 'MongoError',
  [Symbol(mongoErrorContextSymbol)]: {} }
  </code></pre>
  
  <p>After spending long time, I connected using mongo shell:</p>
  
  <pre><code>mongo -u "writeApp" -p writeApp9779 --authenticationDatabase "admin"
  &gt; use writeapp;
    switched to db writeapp
  &gt; db.users.find();
    { "_id" : ObjectId("5cb63f23755c4469f8f6e2e6"),
    ..........
    }
  </code></pre>
  
  <p>It is working in the console but not in the application.I can't find any solution. Really need your kind consideration. By the way here is a sample node.js </p>
  
  <pre><code>Category.find(function(err, categories){
  if(err) console.log(err);
  else {
      app.locals.categories = categories;
  }
  });
  </code></pre>
  
  <p>Model:</p>
  
  <pre><code>var mongoose =require('mongoose');
  var CategorySchema = mongoose.Schema({
  title: {
      type:String,
      required:true
  },
  slug: {
      type:String
     }
  });
  
  module.exports = mongoose.model("Category", CategorySchema);
  </code></pre>