How to Drop or Delete a Collection in MongoDB

Tahseen Tauseef Feb 02, 2024
  1. MongoDB Delete Collections
  2. Use the drop() Method in MongoDB
  3. Use the remove() Method in MongoDB
  4. Delete Collections in MongoDB
  5. Use the drop() Method to Delete Collections
  6. Use remove() Method to Delete All Documents From the Collection
How to Drop or Delete a Collection in MongoDB

This article will discuss the problem of deleting collections in MongoDB. Different methods used in MongoDB to delete collections will be explained in the following.

MongoDB Delete Collections

MongoDB delete collection has two ways. After running the drop command in the MongoDB database server, the output will be in true or false format.

If the collection exists in the database and will be deleted successfully, the result will be true. The result will be false if the collection does not exist in the database and can’t be removed appropriately.

To delete a collection from MongoDB, use the drop and remove method. When removing a collection, you must additionally provide the collection name.

Use the drop() Method in MongoDB

To remove a collection from a database in MongoDB, use the db.collection.drop() method. It eliminates a collection from the database and does not leave any indexes connected with it.

How does the drop() method work:

  1. The db.collection.drop() method and drop command create an invalidate event for any change streams opened on dropped collection.

  2. Trying to drop a collection with in-progress index builds before MongoDB 4.4 results in error, and the collection is not discarded. Aborting a primary index build does not abort the secondary index makes for replica sets or shard replica sets.

    Instead, MongoDB tries to terminate the in-progress builds for the given indexes on the primary and writes an abort oplog item if it succeeds. Before committing or aborting the index build, secondary members with replicated in-progress builds wait for a commit or abort the oplog entry from the main.

  3. Starting in MongoDB 4.0.2, dropping a collection deletes its associated zone/tag ranges.

  4. If you try to drop a collection in the admin database or the config database from a mongos since MongoDB 5.0, the drop command and the db.collection.drop() function will produce an error. Instead, connect to the config server and perform the command to remove these collections.

Syntax:

db.collection_database.drop()

In the above syntax, the collection database is defined as the collection’s name, which is used to delete from the database server.

Use the remove() Method in MongoDB

To delete documents from a collection, use the db.collection.remove() function.

Syntax:

db.collection_database.remove ({})

The collection database is defined as from which it has to remove the documents if you pass the empty result set ({ }) remove method will delete all the records from the collection.

db.collection_name.remove (<query>, {Writeconcern, justone})

The query parameter is defined as the ability to delete specific articles from a collection based on criteria stated in the query. justone argument is defined, which deletes one document from the collection. It must always be enabled by setting the keyword to true.

Only one record is removed after applying the true keyword in the selection criterion. If you didn’t utilize the true keyword in our selection criteria, all of the papers in the collection would be erased.

When utilizing Writeconcern, the default usage of write concern will be skipped.

Delete Collections in MongoDB

Use the drop() method to remove any collection from the database, including its indexes. The collection and its indexes will be removed from the database server using the drop technique.

You can delete certain documents from the collection using the remove technique. The remove method will destroy all of the documents from the collection, but the indexes will remain on the database server and not be erased.

After removing a collection from the database server, two result sets will appear below, true or false.

The following query demonstrates that the result-set will display the true value when a collection is dropped from the database server.

Query:

show collections
db.test1.drop()
show collections

To delete a collection from a database, you must first connect to the database in question. If you have not connected to or accessed the specified database, the collection will not be destroyed.

The below query shows how you need to use the specified database before deleting the collection from the database.

Query:

db.col_stat.drop()
use test
db.col_stat.drop()
show collections

Use the drop() Method to Delete Collections

Use the command show dbs to see a list of accessible databases.

use show dbs command to list accessible database

If you wish to delete a new database mydb>, use the dropDatabase() command as follows:

use dropDatabase() command to delete

Now checklist of databases.

drop the mydb database successfully

Use remove() Method to Delete All Documents From the Collection

Consider the following data from the mycol dataset.

{_id : ObjectId("507f191e810c19729de860e1"), title: "MongoDB Overview"},
{_id : ObjectId("507f191e810c19729de860e2"), title: "NoSQL Overview"},
{_id : ObjectId("507f191e810c19729de860e3"), title: "Tutorials Point Overview"}

The following example will delete all records with the title 'MongoDB Overview'.

deletes all records with the title &lsquo;MongoDB Overview&rsquo;

If there are numerous records and you want to delete the first one, use the justOne option in the remove() function.

>db.COLLECTION_NAME.remove(DELETION_CRITERIA,1)

MongoDB will remove whole documents from the collection if there are no specified deletion criteria. This is the SQL truncate command’s counterpart.

removes whole documents from the collection if no specified criteria

Nothing will be shown after db.mycol.find() as all data is removed from databases.

This article taught us how to drop or remove collections in MongoDB. The drop() method drops the collection and its indexes, while the remove method deletes a single or all the documents from the collection.

If the collection exists in the database, the result-set will show the result as true, displaying a false output.

Related Article - MongoDB Collection