截断集合

扫码查看
本文介绍了截断集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何在MongoDB中截断集合或有这样的事情?

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

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

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.

推荐答案

您可以使用 db.collection.drop() .删除包含大量文档和/或索引的集合比使用db.collection.remove({})删除所有文档要有效得多. remove()方法在删除文档时会做额外的整理索引工作,在副本集环境中操作日志将包括删除的每个文档的条目,而不是单个收集放置命令.在副本集环境中,这甚至会更慢.

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.

使用mongo shell的示例:

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(MongoDB 3.2或更高版本中的默认存储引擎)将在删除完成后释放被删除集合(以及任何相关索引)使用的空间.
  • MMAPv1(MongoDB 3.0及更高版本中的默认存储引擎)将释放预分配的磁盘空间.这可能适合您的用例;插入新数据后,可用空间就可以重复使用.
  • 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) willnot 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.

但是,这里有一个示例,该示例在mongo shell中删除并重新创建具有相同集合名称的数据库:

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

这篇关于截断集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 23:33
查看更多