title: Getting MongoCommandException while connecting to the MongoDB | Java
body: <p>I have created following mongoDB replicaSet:</p>
  
  <pre><code>set: rs0,
  primary: localhost:27018,
  secondry: localhost: 27019
  MongoDB version: 4.2.3
  </code></pre>
  
  <p>When I am trying to connect to the replSet(I have tried connecting to both) using mongodb-java-driver-3.12, I am getting following Error:</p>
  
  <pre><code>com.mongodb.MongoCommandException: Command failed with error 2 (BadValue): 'The field 'key' for an _id index must be {_id: 1}, but got { _id: -1 }' on server localhost:27019. The full response is {"operationTime": {"$timestamp": {"t": 1581572046, "i": 1}}, "ok": 0.0, "errmsg": "The field 'key' for an _id index must be {_id: 1}, but got { _id: -1 }", "code": 2, "codeName": "BadValue", "$clusterTime": {"clusterTime": {"$timestamp": {"t": 1581572046, "i": 1}}, "signature": {"hash": {"$binary": "AAAAAAAAAAAAAAAAAAAAAAAAAAA=", "$type": "00"}, "keyId": {"$numberLong": "0"}}}}
      at com.mongodb.internal.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:175)
      at com.mongodb.internal.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:303)
      at com.mongodb.internal.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:259)
      at com.mongodb.internal.connection.UsageTrackingInternalConnection.sendAndReceive(UsageTrackingInternalConnection.java:99)
      at com.mongodb.internal.connection.DefaultConnectionPool$PooledConnection.sendAndReceive(DefaultConnectionPool.java:450)
      at com.mongodb.internal.connection.CommandProtocolImpl.execute(CommandProtocolImpl.java:72)
      at com.mongodb.internal.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:226)
      at com.mongodb.internal.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:269)
      at com.mongodb.internal.connection.DefaultServerConnection.command(DefaultServerConnection.java:131)
      at com.mongodb.internal.connection.DefaultServerConnection.command(DefaultServerConnection.java:123)
      at com.mongodb.operation.CommandOperationHelper.executeWriteCommand(CommandOperationHelper.java:369)
      at com.mongodb.operation.CommandOperationHelper.executeWriteCommand(CommandOperationHelper.java:360)
      at com.mongodb.operation.CommandOperationHelper.executeCommand(CommandOperationHelper.java:284)
      at com.mongodb.operation.CommandOperationHelper.executeCommand(CommandOperationHelper.java:277)
      at com.mongodb.operation.CreateIndexesOperation$1.call(CreateIndexesOperation.java:177)
      at com.mongodb.operation.CreateIndexesOperation$1.call(CreateIndexesOperation.java:172)
      at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:530)
      at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:492)
      at com.mongodb.operation.CreateIndexesOperation.execute(CreateIndexesOperation.java:172)
      at com.mongodb.operation.CreateIndexesOperation.execute(CreateIndexesOperation.java:72)
      at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:213)
      at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:182)
      at com.mongodb.DBCollection.createIndex(DBCollection.java:1698)
      at org.mongodb.morphia.DatastoreImpl.ensureIndex(DatastoreImpl.java:1110)
      at org.mongodb.morphia.DatastoreImpl.processEmbeddedAnnotations(DatastoreImpl.java:1524)
      at org.mongodb.morphia.DatastoreImpl.ensureIndexes(DatastoreImpl.java:1185)
      at org.mongodb.morphia.DatastoreImpl.ensureIndexes(DatastoreImpl.java:1167)
      at org.mongodb.morphia.DatastoreImpl.ensureIndexes(DatastoreImpl.java:1189)
      at org.mongodb.morphia.DatastoreImpl.ensureIndexes(DatastoreImpl.java:286)
      at org.mongodb.morphia.DatastoreImpl.ensureIndexes(DatastoreImpl.java:280)
      at org.mongodb.morphia.dao.BasicDAO.ensureIndexes(BasicDAO.java:132)
      at com.linkscreens.persistence.dao.generic.GenericMongoDao.&lt;init&gt;(GenericMongoDao.java:46)
      at com.linkscreens.broadcasting.dao.BroadcastHistoryDaoImpl.&lt;init&gt;(BroadcastHistoryDaoImpl.java:29)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
      at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:142)
      ... 71 common frames omitted
  ```
  </code></pre>