Clear or delete a collection in MongoDB
This article will discuss the issue of deleting collections in MongoDB. The following are different ways to delete collections in MongoDB.
MongoDB delete collection
There are two ways to delete a collection in MongoDB. After running the drop command in the MongoDB database server, the output will be in the format of true or false.
The result will be true if the collection exists in the database and will be deleted successfully. The result will be false if the collection does not exist in the database and cannot be deleted appropriately.
To remove a collection from MongoDB, use the drop and remove methods. When removing a collection, you must also provide the collection name.
Using drop() method in MongoDB
To remove a collection from a database in MongoDB, use the db.collection.drop() method. It removes a collection from the database and does not leave any indexes associated with it.
How drop() method works:
- The db.collection.drop() method and the drop command create an invalidate event for any change streams open on the dropped collection.
-
Prior to MongoDB 4.4, attempting to drop a collection with an index build in progress results in an error and the collection is not dropped. Aborting a primary index build does not abort secondary indexes created for a replica set or shard replica set.
Instead, MongoDB attempts to terminate the ongoing build of a given index on the primary, writing an abort oplog entry if successful. Secondary members with replicated ongoing builds wait for the commit or abort oplog entry from the primary before committing or aborting the index build.
- Starting in MongoDB 4.0.2, deleting a collection deletes its associated region/tag ranges.
- Starting in MongoDB 5.0, if you attempt to drop a collection in the admin or config database from a mongos, the drop command and the db.collection.drop() function will produce an error. Instead, connect to the config server and execute commands to drop these collections.
grammar:
db.collection_database.drop()
In the above syntax, collection-database is defined as the name of the collection, which is to be deleted from the database server.
Using remove() method in MongoDB
To remove a document from a collection, use db.collection.remove()
the function.
grammar:
db.collection_database.remove ({})
The collection database is defined as if you pass an empty result set ( { }
) it must delete documents from it, the delete method will delete all records from the collection.
db.collection_name.remove (<query>, {Writeconcern, justone})
The query parameter defines the ability to remove specific articles from the collection based on the conditions specified in the query. The justone parameter is defined, which removes one document from the collection. It must always be enabled by setting the keyword to true.
After applying true keyword in selection criteria, only one record will be deleted. If you do not use true keyword in our selection criteria, all papers in the collection will be deleted.
When using Writeconcern, the default usage of write concern is skipped.
Deleting a collection in MongoDB
drop()
Remove any collection including its indexes from the database
using the remove method. The collection and its indexes will be removed from the database server using the remove technique.
You can use the remove technique to remove certain documents from a collection. The remove method will destroy all the documents in the collection, but the index will remain on the database server and will not be deleted.
After deleting a collection from the database server, two result sets appear below, true or false.
The following query demonstrates that when a collection is deleted from the database server, the result set displays true values.
Query:
show collections
db.test1.drop()
show collections
To delete a collection from a database, you must first connect to the relevant database. If you have not yet connected to or accessed the specified database, the collection will not be destroyed.
The following query shows how the specified database needs to be used before a collection can be deleted from the database.
Query:
db.col_stat.drop()
use test
db.col_stat.drop()
show collections
Use the drop() method to delete a collection.
Use the command show dbs
to view a list of accessible databases.
If you want to delete the new database mydb>
, use dropDatabase()
the command as follows:
Now the database list.
Use the remove() method to remove all documents from a 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 titled "MongoDB Overview".
If there are many records and you want to delete the first record, use remove()
the justOne option in the function.
>db.COLLECTION_NAME.remove(DELETION_CRITERIA,1)
If there is no specified delete condition, MongoDB will delete the entire document from the collection. This is the counterpart to the SQL Truncate command.
db.mycol.find()
Nothing will be displayed afterwards, as all data has been deleted from the database.
This article teaches us how to delete or remove a collection in MongoDB. drop()
The remove method deletes a collection and its indexes, while the remove method removes a single or all documents from a collection.
If the collection exists in the database, the result set will show the result as true, otherwise false output.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Date comparison in MongoDB
Publish Date:2025/04/28 Views:169 Category:MongoDB
-
This MongoDB tutorial discusses the issue of returning Date-based queries. In addition, there is also a good tutorial on how to use Date Range queries in MongoDB. Using date ranges in MongoDB We will learn to write MongoDB date range querie
Find objects between two dates in MongoDB
Publish Date:2025/04/28 Views:94 Category:MongoDB
-
In this article, the problem of finding objects between two dates was briefly discussed. In addition, the operators $gte, $lte, $gt, and $lt used for this purpose were briefly explained in detail. Querying date ranges in MongoDB This sectio
Comparing Dates in MongoDB
Publish Date:2025/04/28 Views:115 Category:MongoDB
-
Date is a common field in most databases, and sometimes we need to find exact documents from a collection in MongoDB. For example, if we have a collection of orders, we might search for those documents before or after a specific date. In th
Convert string to date in MongoDB
Publish Date:2025/04/28 Views:162 Category:MongoDB
-
MongoDB is an excellent platform that is growing in popularity. Among the various features it offers, MongoDB also allows you to convert data from one type to another. This may seem like a complex function, but it is very simple to execute.
Building the MongoDB REST API
Publish Date:2025/04/28 Views:70 Category:MongoDB
-
MongoDB is a flexible and scalable document-oriented database system that is widely used for large-volume data storage. It uses documents and collections instead of the traditional rational database approach of using tables and rows. MongoD
Using ORM with MongoDB
Publish Date:2025/04/28 Views:117 Category:MongoDB
-
MongoDB introduces a NoSQL solution for data storage and management, consisting of documents represented in JSON style. Like other database systems, MongoDB can also use ORM. In this article, we will explain the concepts of ORM and general
Locking mechanism in MongoDB
Publish Date:2025/04/28 Views:95 Category:MongoDB
-
In database management systems, locking mechanisms ensure the consistency of the entire result. For example, if some writing process is in progress on the data, a read command cannot be executed at the same time. Database resources are lock
Unique Index in MongoDB
Publish Date:2025/04/28 Views:130 Category:MongoDB
-
In this article, you'll learn about unique indexes, including what they are and how to create them in MongoDB. Additionally, the process of making a user's email unique in MongoDB is briefly described. The contents of this article are as fo
Creating Indexes in MongoDB
Publish Date:2025/04/28 Views:178 Category:MongoDB
-
Indexes help resolve queries efficiently. Without indexes, MongoDB must iterate through every document in the collection to find the documents that match the query. It will waste time and require MongoDB to handle such information. Therefor