How to Truncate Collection in MongoDB

Shraddha Paghdar Feb 02, 2024
How to Truncate Collection in MongoDB

A collection is nothing more than a folder with all the documents. There is a cap on the number of records you can add to collections when utilizing capped collections.

A document can have a maximum size of 16MB. Occasionally, you wish to build a new collection and remove documents from the current one.

You can select one of the two choices to truncate the collection below based on your needs. In today’s article, we’ll learn how to truncate a collection in MongoDB.

Truncate a collection in MongoDB

We can use either drop() or remove() based on our project requirements. We can use drop() if we want to drop all data & indexes of a collection while remove() deletes matching documents and preserve (update) indexes.

Let’s start with the drop() method below.

Use the drop() Method

In MongoDB, you can remove a collection from a database using the drop() method. A collection is deleted from the database, and any indexes linked to the dumped collections are also deleted.

The db.collection.drop() method throws an error when used with an argument and does not accept any arguments. If the drop command successfully deletes a collection, it returns true.

When there is no collection to drop, it returns false. You can find more information about the drop() method here.

Syntax:

> db.collectionName.drop()

Let’s use the following example to understand the mentioned idea:

> db.users.drop()

We are deleting all of the user collections in the sample before, which will also automatically remove any associated indexes. Run the above line of code in MongoShell, which is compatible with MongoDB.

It will display the following outcome:

true

Use the remove() Method

In MongoDB, you can remove a collection from a database using the remove() method.

However, this method would be considerably slower in a replica set scenario because the oplog would contain entries for each document removed instead of a single collection drop command.

The remove() method is incompatible with capped collections. The remove() method does not apply to a time series’ collection’. Depending on your needs, pick an effective approach.

If the remove() successfully deletes a collection, it returns a WriteResult object containing the operation’s status. You can find more information about the remove() here.

Syntax:

> db.collectionName.remove({})

Let’s use the following example to understand the mentioned idea:

> db.users.remove({})

The complete user collection data is deleted in the sample above, but this method does not remove the indexes linked to the collection. Run the above line of code in MongoShell, which is compatible with MongoDB. It will display the following outcome:

WriteResult({ "nRemoved" : 3300 })
Shraddha Paghdar avatar Shraddha Paghdar avatar

Shraddha is a JavaScript nerd that utilises it for everything from experimenting to assisting individuals and businesses with day-to-day operations and business growth. She is a writer, chef, and computer programmer. As a senior MEAN/MERN stack developer and project manager with more than 4 years of experience in this sector, she now handles multiple projects. She has been producing technical writing for at least a year and a half. She enjoys coming up with fresh, innovative ideas.

LinkedIn

Related Article - MongoDB Collection