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.
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
Syntax:
db.collection_name.drop()
Example:
db.students.drop()