title: Return multiple documents from MongoDB using Java
body: <p>I have been searching through the world wide web for hours and I can't find a viable solution to my problem:</p>
  
  <p>It is simple: I want to search through a collection of items from my MongoDB and return all the documents. </p>
  
  <p>From a RESTFul API perspective : <em>GET /items - Returns all items in the collection. Not sorted</em>  </p>
  
  <pre><code>// NOT WORKING - STILL TRYING  
      public static String getItems() {
  
          StringBuilder items = new StringBuilder();
      MongoCursor&lt;Document&gt; cursor = itemCollection.find().iterator();
      try {
  
          while (cursor.hasNext()) {
              items.append(cursor.next().toJson());
  
          }
  
      } finally {
          cursor.close();
      }
  
      return items.toString();
      }
  </code></pre>
  
  <p>As you can see I return a StringBuilder, since I want to concatenate every document into one big bulk. But this returns as <strong>TEXT</strong> and not <strong>JSON</strong>. See below: </p>
  
  <p>This is when the read type is set as JSON<br>
  <a href="https://i.stack.imgur.com/v9dJa.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/v9dJa.png" alt="When JSON is set as read type"></a></p>
  
  <p>The below is when the read type is set to TEXT, which creates the output I need, but it is not the correct format.  </p>
  
  <p><a href="https://i.stack.imgur.com/ngqef.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ngqef.png" alt="When TEXT is set as read type"></a></p>
  
  <p>I can't return it as a Document and then use the method: <em>toJson()</em>, since it will only return the last entry. 
  I've tried with Lists, but it can't be converted into JSON documents. 
  The above is the closest I get to what I need. </p>
  
  <p>I hope someone in here has been through the same issue as me and can give a quick tip to solve the issue I'm having :-).</p>