Wednesday 4 March 2015

mongoDB : java : Drop collection


Step 1: Get mongo client
Ex:
MongoClient mongoClient = new MongoClient("localhost", 27017);

Step 2: Connect to DB.
Ex:
DB db = mongoClient.getDB("test");

Step 3: Get collection
Ex:
DBCollection collection = db.getCollection("sample");

Step 4: Drop collection
Ex:
collection.drop();


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

public class DropCollection {
  /* Step 1 : get mongoCLient */
  public static MongoClient getMongoClient() {
    MongoClient mongoClient = null;
    try {
      mongoClient = new MongoClient("localhost", 27017);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return mongoClient;
  }
  
  public static void main(String[] args) throws Exception {   
    MongoClient mongoClient = getMongoClient();
    
    /* Step 2: Connect to DB */
    DB db = mongoClient.getDB("test");
    
    /*Step 3 : Get collection */
    DBCollection collection = db.getCollection("sample");
        
    /* Step 4: Drop collection */
    collection.drop();
  }
}

Prevoius                                                 Next                                                 Home

No comments:

Post a Comment