title: How do I fix a com.mongo.MongoSocketReadException, with the message "Prematurely reached the end of Stream" on attempting to insert a document?
body: <p>I am using Spring Boot for my project and I am planning to migrate the user data my application has to MongoDB. Inserting was working for a while, but now all of a sudden, I am receiving this Stack Trace:</p>
  <pre><code>com.mongodb.MongoSocketReadException: Prematurely reached end of stream
      at com.mongodb.connection.SocketStream.read(SocketStream.java:87)
      at com.mongodb.connection.InternalStreamConnection.receiveResponseBuffers(InternalStreamConnection.java:547)
      at com.mongodb.connection.InternalStreamConnection.receiveMessage(InternalStreamConnection.java:418)
      at com.mongodb.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:290)
      at com.mongodb.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:255)
      at com.mongodb.connection.CommandHelper.sendAndReceive(CommandHelper.java:84)
      at com.mongodb.connection.CommandHelper.executeCommand(CommandHelper.java:34)
      at com.mongodb.connection.InternalStreamConnectionInitializer.initializeConnectionDescription(InternalStreamConnectionInitializer.java:91)
      at com.mongodb.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:51)
      at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:127)
      at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:114)
      at java.base/java.lang.Thread.run(Thread.java:832)
  
  </code></pre>
  <p>This is then followed by Mongo attempting to Update the Cluster description, then closing the connection,
  and ended with this stack trace:</p>
  <pre><code>Exception in thread &quot;main&quot; com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches com.mongodb.Mongo$4@75d4a5c2. Client view of cluster state is {type=REPLICA_SET, servers=[{address=sorinorpgcluster-shard-00-00.huchg.mongodb.net:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketReadException: Prematurely reached end of stream}}, {address=sorinorpgcluster-shard-00-01.huchg.mongodb.net:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketReadException: Prematurely reached end of stream}}, {address=sorinorpgcluster-shard-00-02.huchg.mongodb.net:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketReadException: Prematurely reached end of stream}}]
      at com.mongodb.connection.BaseCluster.createTimeoutException(BaseCluster.java:389)
      at com.mongodb.connection.BaseCluster.selectServer(BaseCluster.java:108)
      at com.mongodb.Mongo.getConnectedClusterDescription(Mongo.java:887)
      at com.mongodb.Mongo.createClientSession(Mongo.java:877)
      at com.mongodb.Mongo$3.getClientSession(Mongo.java:866)
      at com.mongodb.Mongo$3.execute(Mongo.java:834)
      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 ProfileMigration.main(ProfileMigration.java:18)
  </code></pre>
  <p>Here is the code:</p>
  <pre><code>import com.mongodb.MongoClient;
  import com.mongodb.MongoClientURI;
  import com.mongodb.client.MongoCollection;
  import com.mongodb.client.MongoDatabase;
  import org.bson.Document;
  
  public class ProfileMigration {
      public static void main(String[] args){
  
         MongoClientURI uri =
                 new MongoClientURI(&quot;mongodb+srv://user:pass@sorinorpgcluster.huchg.mongodb.net/user?retryWrites=true&amp;w=majority&quot;);
  
         MongoClient mongoClient = new MongoClient(uri);
         MongoDatabase db = mongoClient.getDatabase(&quot;user&quot;);
         MongoCollection&lt;Document&gt; collection =  db.getCollection(&quot;account&quot;);
  
  
         collection.insertOne(new Document(&quot;test&quot;, &quot;test&quot;));
      }
  }
  </code></pre>
  <p>I am very confused as to how to fix this, I have tried using MongoClientOptions and that does not seem to work. I am using MongoDB version 3.6.4.</p>