Friday, 6 March 2015

mongoDB : Java : Remove all documents from collection


There are many ways to remove documents from a collection.

Way 1: Get all documents and remove documents one by one

import java.net.UnknownHostException;

import com.mongodb.DBCursor;
import com.mongodb.MongoClient;
import com.mongodb.DB;
import com.mongodb.DBCollection;

public class UpdateDocument {

  /* Step 1 : get mongoClient */
  public static MongoClient getMongoClient(){
    MongoClient mongoClient = null;
     try {
       mongoClient = new MongoClient( "localhost" , 27017 );
    } catch (UnknownHostException e) {
      e.printStackTrace();
    }
     return mongoClient;
  }
  
  public static void main(String args[]){
    MongoClient mongoClient = getMongoClient();
    
    /* Step 2: Connect to DB */
    DB db = mongoClient.getDB("sample");
    
    /*Step 3 : Get collection */
    DBCollection collection = db.getCollection("employee");
    
    System.out.println("Before deleting");
    DBCursor cursor = collection.find();
    while(cursor.hasNext()){
      System.out.println(cursor.next());
    }
    
    cursor = collection.find();
    while(cursor.hasNext()){
      collection.remove(cursor.next());     
    }
    
    System.out.println("After deleting");
    cursor = collection.find();
    while(cursor.hasNext()){
      System.out.println(cursor.next());
    }
  }
  
}


Output
Before deleting
{ "_id" : 1.0 , "firstName" : "Joel" , "lastName" : "chelli"}
{ "_id" : 2.0 , "firstName" : "Ananad" , "lastName" : "Bandaru"}
{ "_id" : 3.0 , "firstName" : "Gopi" , "lastName" : "Battu"}
{ "_id" : 4.0 , "firstName" : "Ritwik" , "lastName" : "Mohenthy"}
After deleting


Way 2: Pass empty object to remove method, it removes all the documents.
collection.remove(new BasicDBObject());

import java.net.UnknownHostException;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCursor;
import com.mongodb.MongoClient;
import com.mongodb.DB;
import com.mongodb.DBCollection;

public class UpdateDocument {

  /* Step 1 : get mongoClient */
  public static MongoClient getMongoClient(){
    MongoClient mongoClient = null;
     try {
       mongoClient = new MongoClient( "localhost" , 27017 );
    } catch (UnknownHostException e) {
      e.printStackTrace();
    }
     return mongoClient;
  }
  
  public static void main(String args[]){
    MongoClient mongoClient = getMongoClient();
    
    /* Step 2: Connect to DB */
    DB db = mongoClient.getDB("sample");
    
    /*Step 3 : Get collection */
    DBCollection collection = db.getCollection("employee");
    
    System.out.println("Before deleting");
    DBCursor cursor = collection.find();
    while(cursor.hasNext()){
      System.out.println(cursor.next());
    }
    
    collection.remove(new BasicDBObject());
    
    System.out.println("After deleting");
    cursor = collection.find();
    while(cursor.hasNext()){
      System.out.println(cursor.next());
    }
  }
  
}


Output
Before deleting
{ "_id" : 1.0 , "firstName" : "Joel" , "lastName" : "chelli"}
{ "_id" : 2.0 , "firstName" : "Ananad" , "lastName" : "Bandaru"}
{ "_id" : 3.0 , "firstName" : "Gopi" , "lastName" : "Battu"}
{ "_id" : 4.0 , "firstName" : "Ritwik" , "lastName" : "Mohenthy"}
After deleting


Way 3: By dropping the collection
collection.drop();

import java.net.UnknownHostException;

import com.mongodb.DBCursor;
import com.mongodb.MongoClient;
import com.mongodb.DB;
import com.mongodb.DBCollection;

public class UpdateDocument {

  /* Step 1 : get mongoClient */
  public static MongoClient getMongoClient(){
    MongoClient mongoClient = null;
     try {
       mongoClient = new MongoClient( "localhost" , 27017 );
    } catch (UnknownHostException e) {
      e.printStackTrace();
    }
     return mongoClient;
  }
  
  public static void main(String args[]){
    MongoClient mongoClient = getMongoClient();
    
    /* Step 2: Connect to DB */
    DB db = mongoClient.getDB("sample");
    
    /*Step 3 : Get collection */
    DBCollection collection = db.getCollection("employee");
    
    System.out.println("Before deleting");
    DBCursor cursor = collection.find();
    while(cursor.hasNext()){
      System.out.println(cursor.next());
    }
    
    collection.drop();
    
    System.out.println("After deleting");
    cursor = collection.find();
    while(cursor.hasNext()){
      System.out.println(cursor.next());
    }
  }
  
}


Output
Before deleting
{ "_id" : 1.0 , "firstName" : "Joel" , "lastName" : "chelli"}
{ "_id" : 2.0 , "firstName" : "Ananad" , "lastName" : "Bandaru"}
{ "_id" : 3.0 , "firstName" : "Gopi" , "lastName" : "Battu"}
{ "_id" : 4.0 , "firstName" : "Ritwik" , "lastName" : "Mohenthy"}
After deleting

Prevoius                                                 Next                                                 Home

No comments:

Post a Comment