Vaulstein Vaulstein - 1 year ago 133
Python Question

How do I drop a MongoDB collection from the command line?

What's the easiest way to do this from my bash prompt?

I know that I can drop a database using below:

mongo <dbname> --eval "db.dropDatabase()"

I want to do the same for a collection preferably in a shell script or python script and not from mongodb console.

What I am trying to achieve is first take a
of the collection and then
the collection:

Command I am using for dump:

mongodump --db database --collection collection_03-11-2016 --out /home/mongodump

Answer Source

If you want to do this in bash one liner. The easiest would be -

mongodump --db yourdatabase --collection yourcollection --out /home/mongodump && mongo --eval "db=db.getSiblingDB('yourdatabase'); db.yourcollection.drop();"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download