
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to create a MongoDB collection using Java?
You can create a collection in MongoDB using the db.createCollection() method.
Syntax
db.createCollection(name, options)
Where
db is the database.
name is the name of the collection you want to create.
Option is the set of optional parameters such as capped, auto indexed, size and, max.
Example
> use myDatabase switched to db myDatabase > db.createCollection("myCollection") { "ok" : 1 }
Using Java program
In Java, you can create a collection using the createCollection() method of the com.mongodb.client.MongoDatabase interface. This method accepts a string value representing the name of the collection.
Therefore to create a collection in MongoDB using Java program −
Make sure you have installed MongoDB in your system
Add the following dependency to its pom.xml file of your Java project.
<dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java-driver</artifactId> <version>3.12.2</version> </dependency>
Create a MongoDB client by instantiating the MongoClient class.
Connect to a database using the getDatabase().
Invoke the createCollection() method by passing the name of the collection(string).
Example
import com.mongodb.client.MongoDatabase; import com.mongodb.MongoClient; public class CreatingCollection { public static void main( String args[] ) { //Creating a MongoDB client MongoClient mongo = new MongoClient( "localhost" , 27017 ); //Connecting to the database MongoDatabase database = mongo.getDatabase("myDatabase"); //Creating a collection database.createCollection("sampleCollection"); System.out.println("Collection created successfully"); } }
Output
Collection created successfully
- Related Articles
- How to drop a MongoDB Collection using Java?
- How to insert a document into a MongoDB collection using Java?
- How to create a new collection in MongoDB?
- How to sort the documents of a MongoDB collection using java?
- How to Update multiple documents in a MongoDB collection using Java?
- How to insert multiple document into a MongoDB collection using Java?
- How to retrieve all the documents from a MongoDB collection using Java?
- How to create a database in MongoDB using Java?
- How to update an existing document in MongoDB collection using Java?
- Create index in a MongoDB collection?
- How to update MongoDB collection using $toLower?
- How to create an index in MongoDB using Java?
- How to limit the number of records, while retrieving data from a MongoDB collection using Java?
- How to create a collection correctly in MongoDB to avoid “ReferenceError: Not defined” error?
- How to drop a collection in MongoDB?
