Contents of page >
- Program to Drop Collection In MongoDb in java >
Program to Drop Collection In MongoDb in java >
package deleteDocumentFromCollectionInMongoDb;
/**
* Write a program to Drop Collection In MongoDb in java
*/
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
public class DropCollectionInMongoDb4 {
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");
/** Drop collection in MongoDB */
//drop method drops/deletes collection from the database
dbCollection.drop();
System.out.println("DROP collection from MongoDb");
System.out.println("Collection has been DROPPED");
}
}
/* OUTPUT
DROP collection from MongoDb
Collection has been DROPPED
*/
|
Download >
Read : How to Import Maven project in eclipse
OR
Summary -
So in this MongoDB tutorial we learned with example and program how to to Drop 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>
Labels:
Delete MongoDB Java
MongoDB Java