MongoDB truncate collection
A collection is nothing but a folder that contains all the documents. When using capped collections, there is a cap on the number of records you can add to the collection.
The maximum size of a document is 16MB. Sometimes you want to build a new collection and delete documents from the current collection.
You can choose one of the two options to truncate the collection below according to your requirement. In today’s article, we will learn how to truncate a collection in MongoDB.
Truncating a collection in MongoDB
drop()
We can use either or
as per our project requirement remove()
. If we want to remove all data and indexes of a collection, we can use drop()
while remove()
to remove the matching documents and keep (update) the indexes.
Let's start with the drop() method below.
Using the drop() method
In MongoDB, you can drop()
remove a collection from a database using the remove method. A collection is deleted from the database, and any indexes linked to the dumped collection are also deleted.
db.collection.drop()
The method throws an error when used with arguments and does not accept any arguments. It returns true if the drop command successfully deleted a collection.
It returns false when there is no collection to drop. You can find more information about the drop() method here.
syntax:
> db.collectionName.drop()
Let's use the following example to understand the above ideas:
> db.users.drop()
We are deleting all user collections from the previous example, which will automatically delete any associated indexes as well. Run the above line of code in a MongoShell that is compatible with MongoDB.
It will display the following results:
true
Using the remove() method
In MongoDB, you can remove()
remove a collection from the database using the method.
However, this approach will be considerably slower in a replica set scenario, as the oplog will contain an entry for each deleted document, rather than a single collection delete command.
remove()
Method is incompatible with capped aggregates. remove()
Method is not applicable to 'aggregates' of time series. Select a valid method based on your needs.
If remove()
a collection is successfully deleted, it returns a WriteResult object containing the status of the operation.
syntax:
> db.collectionName.remove({})
Let's use the following example to understand the above ideas:
> db.users.remove({})
In the above example, the complete user collection data is deleted, but this method does not delete the index linked to the collection. Run the above line of code in a MongoShell compatible with MongoDB. It will display the following result:
WriteResult({ "nRemoved" : 3300 })
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