Java & MongoDB Tutorial
Java & MongoDB Useful Resources
Selected Reading
- Java & MongoDB - Sorting Records
- Java & MongoDB - Limiting Records
- Java & MongoDB - Referenced Documents
- Java & MongoDB - Embedded Documents
- Java & MongoDB - Delete Document
- Java & MongoDB - Update Document
- Java & MongoDB - Select Document
- Java & MongoDB - Insert Document
- Java & MongoDB - Display Collections
- Java & MongoDB - Drop Collection
- Java & MongoDB - Create Collection
- Java & MongoDB - Drop Database
- Java & MongoDB - Show Databases
- Java & MongoDB - Connect Database
- Java & MongoDB - Environment Setup
- Java & MongoDB - Overview
- Java & MongoDB - Home
Java & MongoDB Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Java & MongoDB - Create Collection
Java & MongoDB - Create Collection
To drop a databases, you can use MongoDatabse.createCollection() method to create a collection in the databases.
// Creating a Mongo cpent MongoCpent mongoCpent = MongoCpents.create(); // Connect the database MongoDatabase database = mongoCpent.getDatabase("myDb"); // Create the collection database.createCollection("sampleCollection");
Example
Following is the code snippet to create a collection −
import com.mongodb.cpent.MongoCpent; import com.mongodb.cpent.MongoCpents; import com.mongodb.cpent.MongoDatabase; pubpc class Tester { pubpc static void main(String[] args) { // Creating a Mongo cpent MongoCpent mongoCpent = MongoCpents.create("mongodb://localhost:27017"); // Connect to database MongoDatabase database = mongoCpent.getDatabase("myDb"); // Create the collection database.createCollection("sampleCollection"); System.out.println("Collection created."); } }
Now, let s compile and run the above program as shown below.
$javac Tester.java $java Tester
Output
On executing, the above program gives you the following output.
Collection created.Advertisements