No SQL > Mongo DB > How to check whether a Collection exists
How to check whether a Collection exists
MongoDB is a scalable, high-performance, open source NoSQL database.
It offers Ad hoc queries, Indexing, Replication, Load balancing, File storage (GridFS), Aggregation,
MapReduce and Server-side JavaScript execution.
This requires the library mongo-2.8.0.jar to be present in the classpath.
The following example shows how to verify whether a Collection exists.
try {
//Connect to mongoDB with given IP and PORT
Mongo mongo = new Mongo("localhost", 27017);
//Get the database object
DB database = mongo.getDB("mytestdb");
//Returns a boolean specifying whether the collection exists or not boolean exists = database.collectionExists("students");
System.out.println(exists);