How to Drop or Delete a Collection in a MongoDB Database
- MongoDB Delete Collections
-
Use the
drop()
Method -
Use the
remove()
Method - Delete Collections in MongoDB
-
Delete Collection Using the
drop()
Method -
Delete All Documents From the Collection Using the
remove()
Method - Use Command-Line to Drop Database in MongoDB
In this article, the problem of deleting collections in MongoDB will be discussed in detail, and different methods used in MongoDB to delete collections will be explained with their queries.
MongoDB Delete Collections
MongoDB deletes collection in two ways; the output will be true
or false
after performing the drop
command in the MongoDB database server. The result will be true
if the collection exists in the database and will be successfully removed.
If the collection does not exist in the database and cannot be appropriately deleted, the result will be false
.
Use the drop
and remove
methods to delete a collection from MongoDB. When uninstalling a collection, you must also specify the collection name.
Use the drop()
Method
To delete a collection from a MongoDB database, use the db.collection.drop()
function. It removes a collection from the database and deletes any associated indexes.
-
The
db.collection.drop()
function anddrop
command generates aninvalidate
event for any change streams open on the discarded collection. -
The
db.collection.drop()
method and thedrop
command stop an in-progress index building on the target collection before dropping it. Attempting to dump a collection with in-progress index builds before MongoDB 4.4 results in error, but the collection is not deleted.Aborting a primary index build does not cancel secondary index builds for replica sets or shard replica sets. Instead, MongoDB attempts to stop the in-progress builds for the specified indexes on the primary and, if successful, produces an abort
oplog
item.Secondary members with replicated in-progress builds wait for the main to commit or abort the
oplog
item before committing or aborting the index build. -
Beginning with MongoDB 4.0.2, removing a collection deletes the zone/tag ranges connected.
-
Since MongoDB 5.0, the
drop
command and thedb.collection.drop()
method will return an error if you attempt to drop a collection in the admin database or the config database from amongos
. Connect to the config server instead and run the command to delete these collections.
Syntax:
db.collection_database.drop()
The collection database is specified as the collection’s name in the above syntax, which is used to remove it from the database server.
Use the remove()
Method
To delete documents from a collection, use the db.collection.remove()
function.
Syntax:
db.collection_database.remove ({})
The collection database is defined in the preceding syntax as the location from which the documents must be removed. Therefore, if you supply the remove
method with an empty result set ()
, it will erase all the collection records.
db.collection_name.remove (<query>, {Writeconcern, justone})
The query
parameter is defined as the ability to remove individual items from a collection depending on the query’s parameters.
There is just one parameter defined, and it deletes one document from the collection. It must be enabled by setting the keyword to true
.
Only one record is eliminated after using the true
keyword in the selection criterion. If you did not use the true
keyword in our selection criteria, you would delete all of the articles in the collection.
When using Writeconcern
, the default use of writeconcern
is bypassed.
Parameters of the remove()
method:
query
- Aquery
operator is used to provide deletion criteria. Passing an emptydocument ()
, for example, will delete all documents in a collection.justOne
- If set totrue
, the deletion will be limited to only one document. Remove all documents that match the deletion criterion without utilizing thefalse
default value.writeConcern
- A document expresses the written worry. Leave out the defaultwriteconcern
.
Delete Collections in MongoDB
If you want to remove the whole collection and its indexes, you may use the drop
method to delete any collection from the database, including its indexes. The drop
approach will delete the collection and its indexes from the database server.
Using the remove
approach, you may remove certain documents from the collection. The remove
method deletes all the documents in the collection, but the indexes remain on the database server and are not deleted.
Two result sets will be displayed following the removal of a collection from the database server.
- True
- False
The following query demonstrates that the result-set will display the true
value when a collection is dropped from the database server. For example, the query outcome below is true
.
The result set will show true
because the database server has deleted the collection.
show collections
db.test1.drop()
show collections
The above query result is false
. The output in the result-set will be false
since the collection has not been removed from the database server or does not exist in the database.
You must first connect to the database in question before removing a collection from it. The collection will not be erased if you have not connected to or visited the specified database.
The query below demonstrates how to utilize the specified database before removing the collection from the database.
db.col_stat.drop()
use test
db.col_stat.drop()
show collections
Now, example queries of the remove
and drop
methods are discussed below.
Delete Collection Using the drop()
Method
First, use the command show dbs
to see a list of accessible databases.
If you wish to delete a new database mydb>
, use the dropDatabase()
command.
The checklist of databases.
Delete All Documents From the Collection Using the remove()
Method
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 MongoDB Overview
.
Remove Only One
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)
Remove All Documents
MongoDB will remove whole documents from the collection if there are no specified deletion criteria. This is the SQL truncate
command’s counterpart.
Nothing will be shown after db.mycol.find()
as all data is removed from databases.
Use Command-Line to Drop Database in MongoDB
The simplest way to delete your Mongo database is to run the mongo
shell command from the command line, along with the relevant flags and parameters to tell the mongo
shell that you want to delete a database.
The mongo
shell command, at its most basic, may be used to connect to a specific database instantly. In this example, it is used to connect to the database bookstore
from our server’s bash prompt.
$ mongo bookstore
MongoDB shell version: 3.0.9
connecting to: bookstore
Instead of connecting to our bookstore
database and performing commands from the mongo
shell, you can supply the eval
flag followed by the JavaScript code you want MongoDB to execute. Our database can easily use dumped in one line.
You wish to drop the database in this scenario; therefore, you’ll use the db.dropDatabase()
function to destroy the database to which you’re connected. While it is not required, you will wrap this method with the printjson
function to guarantee that the output of this command makes sense and is understandable.
You don’t need heredocs
or eval
, mongo
can act as an interpreter.
var db = new Mongo().getDB('someDatabase');
db.dropDatabase();
This article explained how to use the drop
method; you can drop the collection and its indexes, and using the remove
method; you can delete 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. Moreover, dropping or deleting a collection in MongoDB using the command line is explained.