title: Write streaming collection to file Java-Mongo
body: <p>I have a server running Monogdb, which also has a collection named <code>mycoll</code>. New documents are inserted to the collection continuously. My intention is to have a local file with a copy of all the documents of the collection. My current approach is the following:</p>
  
  <pre><code>String host = "myHost";
  int port = 3717;
  String user = "user";
  String password = "pass";
  String databaseName = "dbName";
  String collectionName = "mycoll";
  
  MongoCredential credential = MongoCredential.createCredential(user, databaseName, password.toCharArray());
  MongoClient mongoClient = new MongoClient(new ServerAddress(host, port), Arrays.asList(credential));
  MongoDatabase database = mongoClient.getDatabase(databaseName);
  MongoCollection&lt;Document&gt; collection = database.getCollection(collectionName);
  FindIterable&lt;Document&gt; cursor = collection.find();
  
  String path = "./outputData.txt";
  
  
  while(cursor.iterator().hasNext()){
  
          try(FileWriter fw = new FileWriter(path, true);
                  BufferedWriter bw = new BufferedWriter(fw);
                  PrintWriter out = new PrintWriter(bw))
          {
                  out.println(cursor.iterator().next());
          } catch (IOException e) {
                  System.out.println("Error writing to file.");
          }
  
  }
  </code></pre>
  
  <p>I know that I could create a buffer in order to store the data and write it afterwards, but the big problem is still not solved: that the documents are accessed one by one. Therefore, my question is: How could I access several documents at once in order to write them in <code>./outputData.txt</code>?</p>