Delete all Documents From Collection In MongoDb in java



Contents of page >
  • Program to Delete all Documents From Collection In MongoDb in java >

Program to Delete all Documents From Collection In MongoDb in java >
package deleteDocumentFromCollectionInMongoDb;
/**
* Write a program to Delete all Documents From Collection In MongoDb in java
*/
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.WriteResult;
public class DeleteDocumentFromCollectionInMongoDb3_AllDocumentsInCollection {
   public static void main(String[] args) {
          /* Connect to MongoDB */
          MongoClient mongoClient =
                       new MongoClient(new MongoClientURI("mongodb://localhost:27017"));
         
          /* Connect to DATABASE */
          //If in case database doesn't exists, it will be created at runtime
          DB db = mongoClient.getDB("mydb");
                      
          /* Get the COLLECTION (TABLE) */
          //If in case collection (Table) doesn't exists, it will be created at runtime
          DBCollection dbCollection = db.getCollection("Employee");
          /*If collection already exists, you may remove everything
          from collection for perfect OUTPUT of program*/
          //dbCollection.remove(new BasicDBObject()); //Not Mandatory
                      
         
          /* create a BasicDBObject (document /record) */
          BasicDBObject basicDBObject1 = new BasicDBObject();
          basicDBObject1.put("id", 1);
          basicDBObject1.put("name", "Ankit");
         
          /* Insert document/record in COLLECTION in MongoDB */
          dbCollection.insert(basicDBObject1);
         
          /* create a BasicDBObject (document /record) */
          BasicDBObject basicDBObject2 = new BasicDBObject();
          basicDBObject2.put("id", 2);
          basicDBObject2.put("name", "Sam");
         
          /* Insert document/record in collection in MongoDB */
          dbCollection.insert(basicDBObject2);        
          /* Display documents of collection before DELETE in MongoDB */
          System.out.println("Display documents of collection before DELETE in MongoDB");
          DBCursor cursor0 = dbCollection.find();
          while (cursor0.hasNext()) {
                 System.out.println(cursor0.next());
          }
         
          /** At this stage Employee collection looks like this >
               { "_id" : { "$oid" : "585547bbad8dc81b4c7c8144"} , "id" : 1 , "name" : "Ankit"}
               { "_id" : { "$oid" : "585547bbad8dc81b4c7c8145"} , "id" : 2 , "name" : "Sam"}
          */
         
         
          /** Now, let's DELETE All Documents In Collection */
         
          /** Execute DELETE/REMOVE query - To DELETE ALL DOCUMENTs in collection in MongoDB */
          //remove method delete documents from a collection.
          WriteResult deleteResult =
                   dbCollection.remove(new BasicDBObject());
          System.out.println("\nExecute DELETE/REMOVE query- To DELETE ALL DOCUMENTs in collection");
          System.out.println("No of documents deleted = " + deleteResult.getN());
          System.out.println("All documents have been DELETED");
         
   }
}
/* OUTPUT
Display documents of collection before DELETE in MongoDB
{ "_id" : { "$oid" : "585547bbad8dc81b4c7c8144"} , "id" : 1 , "name" : "Ankit"}
{ "_id" : { "$oid" : "585547bbad8dc81b4c7c8145"} , "id" : 2 , "name" : "Sam"}
Execute DELETE/REMOVE query- To DELETE ALL DOCUMENTs in collection
No of documents deleted = 2
All documents have been DELETED
*/


Download >
Now, it’s time to Download maven Project :) Click here.

Read : How to Import Maven project in eclipse

OR



Summary -
So in this MongoDB tutorial we learned with example and program how to to Delete all Documents From Collection In MongoDb in java.


Having any doubt? or you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter. You may join our fbGroup or linkedInGroup as well.


RELATED LINKS>






eEdit
Must read for you :