title: Connecting to MongoDB Atlas: com.mongodb.MongoCommandException: Command failed with error 8000
body: <p>so I'm trying to connect to MongoDB Atlas Service, which is hosting my database (which i have done in the past with no problems), but I keep getting this error and I can't understand why. I cannot find anyone with the same issue, so I'm quite stuck.</p>
  
  <pre><code>Exception in thread "main" com.mongodb.MongoCommandException: Command failed with error 8000: 'not authorized on admin to execute command { insert: "adminCol", ordered: true, documents: [[{_id ObjectIdHex("5a4e3b6dd04f1c047975fdd5")} {id 512} {name peter jones} {hello hi}]] }' on server *******-shard-00-02-xygnn.mongodb.net:27017. The full response is { "ok" : 0, "errmsg" : "not authorized on admin to execute command { insert: \"adminCol\", ordered: true, documents: [[{_id ObjectIdHex(\"5a4e3b6dd04f1c047975fdd5\")} {id 512} {name peter jones} {hello hi}]] }", "code" : 8000, "codeName" : "AtlasError" }
  at com.mongodb.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:164)
  at com.mongodb.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:295)
  at com.mongodb.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:255)
  at com.mongodb.connection.UsageTrackingInternalConnection.sendAndReceive(UsageTrackingInternalConnection.java:98)
  at com.mongodb.connection.DefaultConnectionPool$PooledConnection.sendAndReceive(DefaultConnectionPool.java:441)
  at com.mongodb.connection.CommandProtocolImpl.execute(CommandProtocolImpl.java:76)
  at com.mongodb.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:189)
  at com.mongodb.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:263)
  at com.mongodb.connection.DefaultServerConnection.command(DefaultServerConnection.java:126)
  at com.mongodb.operation.MixedBulkWriteOperation.executeCommand(MixedBulkWriteOperation.java:373)
  at com.mongodb.operation.MixedBulkWriteOperation.executeBulkWriteBatch(MixedBulkWriteOperation.java:255)
  at com.mongodb.operation.MixedBulkWriteOperation.access$700(MixedBulkWriteOperation.java:66)
  at com.mongodb.operation.MixedBulkWriteOperation$1.call(MixedBulkWriteOperation.java:199)
  at com.mongodb.operation.MixedBulkWriteOperation$1.call(MixedBulkWriteOperation.java:190)
  at com.mongodb.operation.OperationHelper.withReleasableConnection(OperationHelper.java:432)
  at com.mongodb.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:190)
  at com.mongodb.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:66)
  at com.mongodb.Mongo$3.execute(Mongo.java:833)
  at com.mongodb.MongoCollectionImpl.executeSingleWriteRequest(MongoCollectionImpl.java:1025)
  at com.mongodb.MongoCollectionImpl.executeInsertOne(MongoCollectionImpl.java:513)
  at com.mongodb.MongoCollectionImpl.insertOne(MongoCollectionImpl.java:493)
  at com.mongodb.MongoCollectionImpl.insertOne(MongoCollectionImpl.java:487)
  at AdminDB.addAdmin(AdminDB.java:51)
  at AdminDB.main(AdminDB.java:76)
  </code></pre>
  
  <p>This is the code I'm using.</p>
  
  <pre><code>public class AdminDB {
      private MongoDatabase adminDB;
      private MongoCollection&lt;Document&gt; adminCollection;
      private String adminCol = "adminCol";
  
      public AdminDB() {
          MongoClientURI uri = new MongoClientURI(
                  "mongodb://***********-shard-00-00-xygnn.mongodb.net:27017,*******-shard-00-01-xygnn.mongodb.net:27017,******-shard-00-02-xygnn.mongodb.net:27017/test?ssl=true&amp;replicaSet=******-shard-0&amp;authSource=admin");
  
          MongoClient mongoClient = new MongoClient(uri);
          adminDB = mongoClient.getDatabase("admin");
          adminCollection = adminDB.getCollection(adminCol);
      }
  
      public void addAdmin(JsonObject json) {
          adminCollection.insertOne(Document.parse(json.toString()));
      }
  
      public static void main(String[] args) {
          AdminDB adminDB = new AdminDB();
          JsonObject json = new JsonObject();
          json.addProperty("id", 512);
          json.addProperty("name", "peter jones");
          json.addProperty("hello", "hi");
  
          adminDB.addAdmin(json);
      }
  }
  </code></pre>