Create and Drop MongoDB Collection

Create and Drop MongoDB Collection

Instructor-svgAl-Mamun Sarkar
Sep 11 , 2020

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()

 

  • Share On:
  • fb
  • twitter
  • pinterest
  • instagram