MongoDB Drop Collection

In this article, We are going to see how to drop collection in MongoDB Server. We will see two different ways to drop collection from MongoDB Database. One is from Linux shell and another is from mongo shell.

MongoDB Drop Collection Syntax:

db.collection.drop()

Examples MongoDB Drop Collection:

1. Create collections

Create empty collection:

use test db.createCollection("inventory")

Create Collection with data:

db.records.insert({"SNO":1,"Name":"George","Age":19}) db.records.insert({"SNO":2,"Name":"Karunakar","Age":18})

2. Verify the collections

show collections

States elchur inventory records

3. Drop a collection from Linux shell

mongo test --eval 'db.inventory.drop()'

Output:

root@mongodb2:/home# mongo test --eval 'db.inventory.drop()' MongoDB shell version v4.2.1 connecting to: mongodb://127.0.0.1:27017/test?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("15a1dcc1-3d2a-4c9b-8188-8912fb3de690") } MongoDB server version: 4.2.1 true

4. Drop a collection from Mongo shell

> use test switched to db test > show collections States elchur records > db.records.drop() true > show collections States elchur

MongoDB Drop Collection