MongoDB Truncate Collection
MongoDB is one of the most popular NoSQL databases that focuses on performance and ease of use. You will find MongoDB used extensively in web development for storing large volumes of non-relational data.
Unlike a traditional SQL database that organizes data into tables, columns, and rows, MongoDB stores data in collections where each collection is like a table in RBDMS. The data stored in the collection is schema-less and can follow any supported format.
However, like any database, you might come across an instance where you need to remove all the data stored in a given collection without actually removing the collection itself.
The goal of this tutorial is to show you the various methods and techniques that you can use to truncate a collection in MongoDB using very simple steps.
NOTE: The methods discussed in this post are very DESCTRUCTIVE and will remove all the data stored in any specified collection. Ensure to backup your data and use the commands wisely.
Prerequisites
Ensure you have the following:
- A working MongoDB server
- Basic knowledge of MongoDB operations
- Access to a MongoDB client or shell for running commands
Method 1 - Using the db.collection.remove()
Method
The most common and straightforward ways of truncating a MongoDB collection is using the remove()
method.
The following shows the syntax of the method:
db.collection.remove(query, justOne)
To remove all the documents in a given collection, we specify an empty query using an empty pair of curly braces {}
as shown in the example below:
Suppose we have a collection called films
. we can truncate it by running the command:
db.films.remove({})
The command above will delete all documents from the films
collection.
Method 2 - Using the db.collection.deleteMany()
Method
Another method we can use to truncate the data stored in a given MongoDB collection is the db.collection.deleteMany()
function.
This function will delete all the documents that match the specified filter. Hence, to truncate the collection, we can pass an empty filter which will match all the documents in that collection.
The method syntax is as shown below:
db.collection.deleteMany(filter)
If we use the films
collection as an example, we can truncate it by running the query:
db.films.deleteMany({})
Similarly, the command will remove all the documents from the films
collection.
Method 3 - Using the db.collection.drop()
and db.createCollection()
Methods
A more unorthodox method of truncating a MongoDB collection is pairing the db.collection.drop()
and the db.createCollection()
methods.
As you can guess, this method involves dropping the entire collection and then re-creating it in subsequent steps.
The syntax for the drop()
function is as shown:
db.collection.drop()
For the db.createCollection()
function:
db.createCollection(name, options)
Let us take the films
collection from earlier, we can remove this collection and then re-create it as shown in the queries below:
db.students.drop()
db.createCollection("films")
As you can guess, this will drop the films
collection and then create a new one with a similar name, effectively truncating it.
Method 4 - Using the db.runCommand()
Method with {emptycapped:1}
If you are working with a capped MongoDB collection, you can use the emptycapped
method to simply empty all the documents in that collection.
The command syntax is as shown:
db.runCommand({ emptycapped : "collection" })
Taking the films
collection as an example, we can truncate it in a simple command as shown:
db.runCommand({ emptycapped : "films" })
Conclusion
In this tutorial, we looked at four main methods of truncating a MongoDB collection. Whether you need to delete all documents to clean up after testing, reset your data, or maintain your collections, you now have four methods of doing exactly that.