MongoDB Create Collections – Delete Collections Tutorial

MongoDB Create Collections – Delete Collections Tutorial from Coding Compiler. In this Create Collection in MongoDB tutorial, we will discuss how to create a collection and how to delete collections in MongoDB. Let’s start learning MongoDB.

MongoDB Create Collections Tutorial

In this MongoDB tutorial, we show you how to use MongoDB to create collections.

What is a MongoDB collection?

A grouping of MongoDB documents. A collection is the equivalent of an RDBMS table. A collection exists within a single database. Collections do not enforce a schema. Documents within a collection can have different fields.

What is capped collection in MongoDB?

We can create collections in mongoDb on which we can apply size limit. These special type of collections are called Capped Collections. These are a kind of circular queues, in which if allocated size limit is reached, it makes space for new documents by overwriting the oldest documents in the collection.

How To Create Collection In MongoDB

The collection is created in MongoDB using the createCollection() method.

Syntax format:

db.createCollection(name, options)

Parameter Description:

  • Name: the name of the collection to create
  • Options: optional parameters, specify options for memory size and index

Options can be as follows:

Field Types of description
capped Boolean (Optional) If true, creates a fixed collection. A fixed collection is a collection of fixed sizes that automatically overwrites the oldest document when it reaches its maximum value. 
When the value is true, the size parameter must be specified.
autoIndexId Boolean (Optional) If true, an index is automatically created in the _id field. The default is false.
size Numerical value (Optional) Specify a maximum value (in bytes) for the fixed collection. 
This field also needs to be specified if capped is true.
max Numerical value (Optional) Specifies the maximum number of documents that are included in the fixed collection.

When you insert a document, MongoDB first checks the size field of the fixed collection and then checks the max field.


Create a runoob collection in the test database:

> use test
switched to db test
> db.createCollection("runoob")
{ "ok" : 1 }

If you want to see an existing collection, you can use the show collections command:

> show collections

Here’s how to use createCollection() with a few key parameters:

Create a fixed collection mycol, the entire collection space size is 6142800 KB, and the maximum number of documents is 10000.

> db.createCollection("mycol", { capped : true, autoIndexId : true, size : 
   6142800, max : 10000 } )
{ "ok" : 1 }

In MongoDB, you don’t need to create a collection. When you insert some documents, MongoDB automatically creates the collection.

> db.mycol2.insert({"name" : "Rookie Tutorial"})
> show collections

MongoDB Delete Collection Tutorial

Here in this section we show you how to use MongoDB to delete collections.

The drop() method is used in MongoDB to remove collections.

Syntax format:


Parameter Description:

  • no

return value

The drop() method returns true if the selected collection was successfully deleted, otherwise it returns false.


In the database mydb, we can first view the existing collections by using the show collections command:

>use mydb
switched to db mydb
>show collections

Then delete the collection mycol2 :


View the collections in the database mydb again with show collections:

>show collections

It can be seen from the results that the mycol2 collection has been deleted. 

Related MongoDB Tutorials For Beginners

What is MongoDB Tutorial For Beginners

MongoDB Basics Tutorial

MongoDB Update Tutorial

MongoDB Query Tutorial

MongoDB Data Modelling Tutorial

MongoDB Scenarios

MongoDB Aggregation Tutorial

MongoDB Performance and Tools

What is NoSQL

MongoDB Installation Tutorial

MongoDB Concept Parsing

MongoDB Connections Tutorial

MongoDB Create Database Tutorial

Leave a Comment