Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to drop or delete a collection in MongoDB?

What is the best way to drop a collection in MongoDB?

I am using the following:

db.collection.drop() 

As described in the manual:

db.collection.drop()

Removes a collection from the database. The method also removes any indexes associated with the dropped collection. The method provides a wrapper around the drop command.

But how can I drop it from the command line?

like image 481
fedorqui 'SO stop harming' Avatar asked Jun 19 '15 22:06

fedorqui 'SO stop harming'


People also ask

How do I delete collections?

Delete a collectionAt the bottom, tap Collections. Tap a collection. ​Delete collection. Check the box, then tap Delete.

Which method is used to delete a collection in MongoDB?

MongoDB's remove() method is used to remove a document from the collection. remove() method accepts two parameters. One is deletion criteria and second is justOne flag. deletion criteria − (Optional) deletion criteria according to documents will be removed.

How do I drop a collection in MongoDB Atlas?

The drop command removes the specified collection or view from the federated database instance storage configuration. Use the wildcard "*" to remove all collections generated by the wildcard collection function (that is, collectionName() ), including the wildcard collection rule itself.

What is drop collection in MongoDB?

In MongoDB, db. collection. drop() method is used to drop a collection from a database. It completely removes a collection from the database and does not leave any indexes associated with the dropped collections.


2 Answers

So either of these are valid ways to do it:

mongo <dbname> --eval 'db.<collection>.drop()' #     ^^^^^^^^            ^^^^^^^^^^^^ 

db.<collection>.drop() #  ^^^^^^^^^^^^ 

For example, for a collection mycollection in a database mydb you would say:

mongo mydb --eval 'db.mycollection.drop()' 

db.mycollection.drop() 

This is the way I fully tested it, creating a database mydb with a collection hello.

  • Create db mydb:

    > use mydb switched to db mydb 
  • Create a collection mycollection:

    > db.createCollection("mycollection") { "ok" : 1 } 
  • Show all the collections there:

    > db.getCollectionNames() [ "mycollection", "system.indexes" ] 
  • Insert some dummy data:

    > db.mycollection.insert({'a':'b'}) WriteResult({ "nInserted" : 1 }) 
  • Make sure it was inserted:

    > db.mycollection.find() { "_id" : ObjectId("55849b22317df91febf39fa9"), "a" : "b" } 
  • Delete the collection and make sure it is not present any more:

    > db.mycollection.drop() true > db.getCollectionNames() [ "system.indexes" ] 

This also works (I am not repeating the previous commands, since it is just about recreating the database and the collection):

$ mongo mydb --eval 'db.mycollection.drop()' MongoDB shell version: 2.6.10 connecting to: mydb true $ 
like image 179
fedorqui 'SO stop harming' Avatar answered Sep 27 '22 23:09

fedorqui 'SO stop harming'


To drop a collection users of database mydb:

> use mydb > db.users.drop() 
like image 33
martinho Avatar answered Sep 27 '22 22:09

martinho