MongoDB

How Does db.createCollection() Work in MongoDB?

MongoDB is a powerful document-oriented Non-Relational database to store numerous BSON format documents. The developer widely uses it due to its flexibility, scalability, multiple data type support, and ease of use. It comprises many built-in methods to perform simple and even complex tasks related to managing databases, such as creating a database, collecting, and deleting them.

This blog will cover the enlisted topics:

Overview of “db.createCollection()” Method in MongoDB

In MongoDB, the “db.createCollection()” Method creates a new collection in the database. The collections serve the same purpose as tables do in RDBMS, but they are flexible and can store multiple data types in documents. However, if a collection with the same name already exists, the “db.createCollection()” method will not create a new collection with that name but rather will return a reference to the existing collection.

Let us see the basic syntax of this method:

db.createCollection(<Collection_Name>, Options)

Here in the above syntax:

  • The “Collection_Name” is the name of the collection that needs to be created. It should be a string value and cannot include any special character.
  • The “Options” parameters are optional arguments, such as “capped”, “size”, “max”, “autoIndex”, and “indexOptionDefaults” to configure the collection.

How Does the “db.createCollection()” Work in MongoDB?

To see how the “db.createCollection()” method works and creates a new collection in MongoDB, follow the steps mentioned below:

Step 1: Connect to MongoDB Server Using MongoDB Shell

Search and open “Command Prompt” from the Start Menu and type this command to connect to the MongoDB server using MongoDB Shell:

mongosh

Output

The MongoDB server is connected successfully.

Step 2: Retrieve the Names of Available Databases

To list the names of databases, execute the “show dbs” command as below:

show dbs

Output

The output returned the names of all databases in the MongoDB server.

Step 3: Switch to a Desired MongoDB Database

The next step is to change the current database with the one where users want to create a new collection:

use Linuxhint

Output

The output returned the message which says, “already on Linuxhint”.

 Step 4: Display the Names of Existing Collections

To list the names of existing collections in the current database, type the command given below:

show collections

Output

The output returned the name of the existing collection name.

Step 5: Create a New Collection using the “db.createCollection()” Method

Provide the name of the collection as the argument that users want to create. For instance, the “Linuxhint_collection” is considered as below:

db.createCollection("Linuxhint_collection")

Output

The output depicted an “ok” message.

Step 6: To Verify the Result of “db.createCollection()” Method

To verify whether a new collection is created in the current database or not. Use the below command to list all available collection names:

show collections

Output

The output displays that a new collection has been created successfully using the “db.createCollection()” method in MongoDB.

Bonus: Managing a Collection in MongoDB

Let us learn some more related and basic tasks for managing a collection in MongoDB.

Create a Document in a Specified Collection

To create a new document in a collection, utilize the “db.collection.insertOne()” method and provide the field-and-value pairs of data for storage. A command to add a document in the newly created collection is provided below:

db.Linuxhint_collection.insertOne({"title": "Introduction to Linuxhint","author": "Nimrah CH","tags": ["Tutorial", "Linuxhint"]})

Output

The document is successfully inserted into the collection.

View Existing Documents in the Collection

To retrieve the existing documents in a collection, use the below command:

db.Linuxhint_collection.find()

Note: Make sure to replace the collection name accordingly.

Output

The output returned the existing documents in the collection.

Delete a Collection from MongoDB

To delete an existing collection from the MongoDB database, use the drop() method:

db.Linuxhint_collection.drop()

The command deletes the “Linuxhint_collection” collection from MongoDB.

Output

The success message appeared after the deletion of the “Linuxhint_collection” collection from the MongoDB Database.

Conclusion

In MongoDB, the “db.createCollection()” method creates a new collection in the current database but only if no already existing collection has the same name. To utilize this method, the user must connect to a MongoDB server and switch to the desired database. After that, create a collection by executing this method. This post has discussed the working of the “db.createCollection()” method in MongoDB.

About the author

Nimrah Ch

I’m a technical writer with a Bachelor's in Software Engineering and Certification in Cyber Security. Through my research and writing, I strive to provide readers with in-depth, informative articles that can help them make informed decisions about their technological needs.