title: How to delete multiple documents in mongodb using java
body: <p>I have a java program that finds documents based on certain date condition and removes all of them. I am not sure how to remove those documents once found. When I query the MongoDB database using following command it shows me <code>2706</code> documents present.</p>
  
  <p><strong>MongoDB version: 3.0</strong></p>
  
  <p><strong>Mongo-Java driver version: 3.1.0</strong></p>
  
  <p><strong>Mongo Shell command:</strong></p>
  
  <pre><code>db.jobs.find({"startTime" : { $gte : "2015-11-04 00:00:00"}}).count();
  Output: 2706
  </code></pre>
  
  <p>I have the following java program that does more or less similar to the above command.</p>
  
  <p><strong>Java code:</strong></p>
  
  <pre><code>MongoDatabase database = DataSourceFactory.getDatabase(DatabaseType.MONGODB.name());
  MongoCollection&lt;Document&gt; collection = database.getCollection("jobs");
  //find and delete existing documents first
  String currentDate = GenericUtils.getCurrentDate() + " 00:00:00";
  Document doc = collection.findOneAndDelete(gte("startTime", currentDate));
  </code></pre>
  
  <p>I am not sure if usage of <code>findOneAndDelete()</code> method is the way to go.  Do I have to write a loop that does this? Is there a way to find and remove all the records in a single shot? </p>
  
  <p>Not sure how to remove all the records and get a count of how many got deleted.</p>
  
  <p>I googled and found some online tutorials but most of them are based on older versions <code>2.1, 2.2</code> etc. </p>
  
  <p>Please guide.</p>