MongoDB Create Collection | MongoDB Drop Collection

Job-ready Online Courses: Knowledge Awaits – Click to Access!

In previous articles, we have seen how to create database. We know that there is no command in MongoDB to create a database. Databases are created automatically in it.

However, there is a command to create a collection. Moreover, we will learn MongoDB create collection. Moreover, we will also learn MongoDB drop collection with examples.

So, let’s start with MongoDB Create Collection | MongoDB Drop Collection.

MongoDB Create Collection

When we talk about relational databases there are tables. But in MongoDB, there are collections instead. In MongoDB, collections are created automatically when we refer it in any command. MongoDB will create it automatically if it doesn’t exist already.

MongoDB Create Collection | MongoDB Drop Collection

MongoDB Create Collection | MongoDB Drop Collection

Example of Create Collection in MongoDB

db.dataflair.insert({
     name: “prachi”
     })

Above command will create a collection with name “dataflair” if it doesn’t exist. It will be created implicitly. We can also create collection explicitly using “createCollection()” command. We will have to follow the syntax as discussed below.

db.createCollection(name, options)

Before going further we need to know the difference between collections and cappedCollection. The collection has no limitation of size, whereas cappedCollection do. We can specify the size and the maximum number of documents that can be created.

MongoDB Create Collection has following parameters:

Parameter   typedescription
Name             stringThe name of the new collection
OptionsdocumentOptional. Configuration options for creating a                                                      capped collection.

Here, the options document contains following fields.

Field Type Description
cappedbooleanOptional. To create a capped collection, Specify true. If you specify true, the size parameter has to be specified.
autoindexidbooleanOptional. The default value is false. Disables automatic creation of an index on the _id field.
 sizenumberOptional. Specifies the maximum size for capped collection. MongoDB removes the older documents once the max size limit is reached.
maxnumberOptional. The maximum no. of documents allowed in a capped collection. Size has precedence over this.
validatordocumentOptional. Allows the user to specify validation rules for the collection
Validation levelstringOptional. Determines the strictness of validation. Use “off”, “strict” and “moderate”.

Let’s see an example for MongoDB create collection.

>use dataflair
switched to db dataflair
>db.createCollection(“mongodb”,
{ capped:true, size:1000000, max:2})
{ “ok” : 1 }

To confirm, execute show collections command.

>show collections
mongodb

This means that a collection named MongoDB has been created.

MongoDB Drop Collection

After learning to MongoDB create collection, let’s see how can we drop collection in MongoDB. MongoDB Drop Collection is even more easy than creating it. To drop a collection, we need to execute the following command.

db.collection_name.drop()

It will return true if the collection is successfully dropped from MongoDB.
Again, you should check it first if your collection exists or not. For that use show collections command.

>use dataflair
Switched to db dataflair
>show collections
mongodb

Now for MongoDB Drop collection, follow the below syntax-

>db.mongodb.drop()
>true

Now you have successfully dropped the collection.

So, this was all about MongoDB Create Collection Tutorial. Hope you like our explanation of MongoDB Drop Collection.

Conclusion

Hence, we have seen how MongoDB creates collections and MongoDB drops collections. Just keep executing some lines of code and everything happens automatically. Now, we will move further creating some more basic concepts on MongoDB. In the next article, we will discuss the data types in MongoDB.

You give me 15 seconds I promise you best tutorials
Please share your happy experience on Google

courses

DataFlair Team

The DataFlair Team provides industry-driven content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our expert educators focus on delivering value-packed, easy-to-follow resources for tech enthusiasts and professionals.

Leave a Reply

Your email address will not be published. Required fields are marked *