MongoDB Create Collection | MongoDB Drop Collection


1. Objective

In previous articles, we have seen how to create database and drop database in MongoDB. 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. In this MongoDB article, 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 | MongoDB Drop Collection

MongoDB Create Collection | MongoDB Drop Collection

2. 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

For example

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.

Let’s Explore Features of MongoDB 

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
capped

 

 

 

 

 

booleanOptional. 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.

Read About Advantages and Disadvantages of MongoDB

3. 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.

This was all about MongoDB Create Collection Tutorial. Hope you like our explanation of MongoDB Drop Collection.

4. 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.

For reference

Leave a comment

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