Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongo: drop collection using the terminal

I want to import data into a couple of collections. This is what I do

$> mongoimport -d myDB -c myCollection --file file.json       

The problem is that this will only work if the collection is empty, otherwise I get errors like

Mon Mar 31 10:45:52.696 E11000 duplicate key error index:.....

Because I want to do this in a bash script I need to clear/drop the collections first. How can I do this in a bash script ?

like image 525
Jeanluca Scaljeri Avatar asked Mar 31 '14 08:03

Jeanluca Scaljeri


2 Answers

Straight from the manual page, use the --drop option:

--drop

Modifies the import process so that the target instance drops every collection before importing the collection from the input.

So you can remove the collection you are importing into and start new.

mongoimport -d myDB -c myCollection --drop --file file.json  
like image 173
Neil Lunn Avatar answered Oct 20 '22 22:10

Neil Lunn


Call the db.collection.drop() method on a collection to drop it from the database.

In bash use mongo <dbname> --eval "db.<collectionName>.drop()"

like image 8
madmxg Avatar answered Oct 20 '22 22:10

madmxg