MongoDB Drop, Truncate and Delete Collection

Delete, Drop and Truncate collections in MongoDB
We will perform all the mongoDB Aggregation Operator on studentInfo DB. Create studentInfo Database in MongoDB server. Use the below code for creating the DB.

  

In the above studentInfo Mongo Database we will create many collection and we will perform mongoDB Delete Collection.

Use command db.createCollection("collectionName")  , this query we will use and create collection in the studentInfo DB.

Below is the video reference for dropping and deleting the Collection in Mongo.




Create Collection In studentInfo DB, below is the query to create the collection.

db.createCollection("studentPersonal") 

db.createCollection("studentMarks")

studentPersonal, studentMarks Collection will be created using above two Mongo Query.

Mongodb Drop Collection Query


Now we will use db.collectionName.drop()  for dropping collection in MongoDB. So as we created the collection named with studentPersonal and studentMarks. MongoDB delete and Drop collection will perform in these two collection.
db.studentPersonal.drop()
db.studentMarks.drop()
This above two query will delete the studentPersonal and studentMarks collection from MongoDB.

Note : If there will not be a single collection in the mongoDB then the studentInfo DB will not be visible.

Mongodb Truncate Collection Query

Sometime you want to truncate the Collection in MongoDB instead of Dropping or Deleting the Collection. In that case you must use remove() method on the collection which you want to truncate.


Syntax for MongoDB Collection Truncate 

db.collection.remove(<query>,<justOne>)
So if we want to truncate the collection we must have to use remove() method. <justOne > is optional here which limits the deletion of document.

We will Truncate the teacherInfo Collection using below command.
db.teacherInfo.remove({})
This above query will truncate the collection in MongoDB.

Our Feature Post

There is a tree between houses of A and B If the tree leans on As House

    There is a tree between houses of A and B. If the tree There is a tree between houses of A and B. If the tree leans on A’s House, the t...

Our Popular Post