Create a Collection in MongoDB
To create a collection in MongoDB use the createCollection() method. Collection name is passed as argument to the method.
Syntax
db.createCollection(‘ collection_name’ );
Example
Creating collection named “Student” in MongoDB
db.createCollection('Student');
Explanation:
In the above example, Student Collection is created using createCollection() method.
Note: In MongoDB, a collection is also created directly when we add one or more documents to it.
How to Create Database and Collection in MongoDB
MongoDB stores data records as documents that are stored together in collections and the database stores one or more collections of documents.
In this article, we will look at methods to create a database and collection in MongoDB.
Contact Us