截断集合

如何在 MongoDB 中截断一个集合,或者有这样的东西吗?

现在我必须一次性删除6个大的集合,我要停止服务器,删除数据库文件,然后重新创建数据库和其中的集合。有没有办法删除数据并保持集合的原样?删除操作需要很长时间。我收藏了数以百万计的作品。

102072 次浏览

Create the database and the collections and then backup the database to bson files using mongodump:

mongodump --db database-to-use

Then, when you need to drop the database and recreate the previous environment, just use mongorestore:

mongorestore --drop

The backup will be saved in the current working directory, in a folder named dump, when you use the command mongodump.

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);
})

To truncate a collection and keep the indexes use

 db.<collection>.remove({})

the below query will delete all records in a collections and will keep the collection as is,

db.collectionname.remove({})

There is no equivalent to the "truncate" operation in MongoDB. You can either remove all documents, but it will have a complexity of O(n), or drop the collection, then the complexity will be O(1) but you will loose your indexes.

The db.drop() method obtains a write lock on the affected database and will block other operations until it has completed.

I think using the db.remove({}) method is better than db.drop().

remove() is deprecated in MongoDB 4.

You need to use deleteMany or other functions:

db.<collection>.deleteMany({})