Create and Drop MongoDB Collection

Author: Al-mamun Sarkar Date: 2020-09-11 10:13:29

In this lesson, I will show you how to create a collection under a database on MongoDB as well as how to drop a collection from the MongoDB server. 

 

Create a collection:

 

 Run mongo command to enter into mongo shell:

mongo

Syntax:

db.createCollection(name, options)

Example:

Select Database:

user school

Create the collection:

db.createCollection("users")

Create with options:

db.createCollection("students", { capped : true, autoIndexID : true, size : 6142800, max : 10000 } )

 

You can insert data without creating the collection. The collection will be created at the time of insertion if the collection doesn't exist. 

Display all collections:

show collections

 

Drop Collection:

Syntax:

db.collection_name.drop()

Example:

db.students.drop()