Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Truncate a collection

How do I truncate a collection in MongoDB or is there such a thing?

Right now I have to delete 6 large collections all at once and I'm stopping the server, deleting the database files and then recreating the database and the collections in it. Is there a way to delete the data and leave the collection as it is? The delete operation takes very long time. I have millions of entries in the collections.

like image 732
iefpw Avatar asked May 11 '13 04:05

iefpw


2 Answers

To truncate a collection and keep the indexes use

 db.<collection>.remove({}) 
like image 79
astroanu Avatar answered Oct 06 '22 01:10

astroanu


You can efficiently drop all data and indexes for a collection with db.collection.drop(). Dropping a collection with a large number of documents and/or indexes will be significantly more efficient than deleting all documents using db.collection.remove({}). The remove() method does the extra housekeeping of updating indexes as documents are deleted, and would be even slower in a replica set environment where the oplog would include entries for each document removed rather than a single collection drop command.

Example using the mongo shell:

var dbName = 'nukeme'; db.getSiblingDB(dbName).getCollectionNames().forEach(function(collName) {     // Drop all collections except system ones (indexes/profile)     if (!collName.startsWith("system.")) {         // Safety hat         print("WARNING: going to drop ["+dbName+"."+collName+"] in 5s .. hit Ctrl-C if you've changed your mind!");         sleep(5000);         db[collName].drop();     } }) 

It is worth noting that dropping a collection has different outcomes on storage usage depending on the configured storage engine:

  • WiredTiger (default storage engine in MongoDB 3.2 or newer) will free the space used by a dropped collection (and any associated indexes) once the drop completes.
  • MMAPv1 (default storage engine in MongoDB 3.0 and older) will not free up preallocated disk space. This may be fine for your use case; the free space is available for reuse when new data is inserted.

If you are instead dropping the database, you generally don't need to explicitly create the collections as they will be created as documents are inserted.

However, here is an example of dropping and recreating the database with the same collection names in the mongo shell:

var dbName = 'nukeme';  // Save the old collection names before dropping the DB var oldNames = db.getSiblingDB(dbName).getCollectionNames();  // Safety hat print("WARNING: going to drop ["+dbName+"] in 5s .. hit Ctrl-C if you've changed your mind!") sleep(5000)  db.getSiblingDB(dbName).dropDatabase();  // Recreate database with the same collection names oldNames.forEach(function(collName) {     db.getSiblingDB(dbName).createCollection(collName); }) 
like image 33
Stennie Avatar answered Oct 06 '22 02:10

Stennie