MongoDB Create Collections – Delete Collections Tutorial

MongoDB Tutorials

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:

FieldTypes ofdescription
cappedBoolean(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.
autoIndexIdBoolean(Optional) If true, an index is automatically created in the _id field. The default is false.
sizeNumerical value(Optional) Specify a maximum value (in bytes) for the fixed collection. 
This field also needs to be specified if capped is true.
maxNumerical 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.

Instance

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
runoob
system.indexes

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

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:

db.collection.drop()

Parameter Description:

  • no

return value

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

Instance

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
mycol
mycol2
system.indexes
runoob
>

Then delete the collection mycol2 :

>db.mycol2.drop()
true
>

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

>show collections
mycol
system.indexes
runoob
>

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 Reply

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