Vaulstein Vaulstein - 3 days ago 5
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
mongodump
of the collection and then
drop
the collection:

Command I am using for dump:

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

Answer

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();"
Comments