mongodb 截断一个集合
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16493902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Truncate a collection
提问by iefpw
How do I truncate a collection in MongoDB or is there such a thing?
我如何在 MongoDB 中截断一个集合或者是否有这样的事情?
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.
现在我必须一次删除 6 个大集合,我正在停止服务器,删除数据库文件,然后重新创建数据库和其中的集合。有没有办法删除数据并使集合保持原样?删除操作需要很长时间。我的收藏中有数百万个条目。
采纳答案by Stennie
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.
您可以使用 有效地删除集合的所有数据和索引db.collection.drop()
。删除包含大量文档和/或索引的集合将比使用db.collection.remove({})
. remove()
当文档被删除时,该方法会执行额外的更新索引的内务处理,并且在副本集环境中会更慢,其中 oplog 将包含每个被删除文档的条目,而不是单个集合 drop 命令。
Example using the mongo
shell:
使用mongo
外壳的示例:
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 notfree up preallocated disk space. This may be fine for your use case; the free space is available for reuse when new data is inserted.
- WiredTiger(MongoDB 3.2 或更高版本中的默认存储引擎)将在删除完成后释放被删除集合(以及任何关联的索引)使用的空间。
- MMAPv1(MongoDB 3.0 及更早版本中的默认存储引擎) 不会释放预先分配的磁盘空间。这可能适合您的用例;插入新数据时,可用空间可重复使用。
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:
但是,以下是在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);
})
回答by astroanu
To truncate a collection and keep the indexes use
要截断集合并保留索引,请使用
db.<collection>.remove({})
回答by IAmHomes
the below query will delete all records in a collections and will keep the collection as is,
下面的查询将删除集合中的所有记录,并保持集合原样,
db.collectionname.remove({})
回答by Alon
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.
MongoDB 中没有与“截断”操作等效的操作。您可以删除所有文档,但它的复杂度为 O(n),或者删除集合,那么复杂度将为 O(1),但您将丢失索引。
回答by vinipsmaker
Create the database and the collections and then backup the database to bson files using mongodump:
创建数据库和集合,然后使用 mongodump 将数据库备份到 bson 文件:
mongodump --db database-to-use
Then, when you need to drop the database and recreate the previous environment, just use mongorestore:
然后,当您需要删除数据库并重新创建以前的环境时,只需使用 mongorestore:
mongorestore --drop
The backup will be saved in the current working directory, in a folder named dump, when you use the command mongodump.
当您使用命令 mongodump 时,备份将保存在当前工作目录中名为 dump 的文件夹中。
回答by Congwu Zhang
The db.drop()
method obtains a write lock on the affected database and will block other operations until it has completed.
该db.drop()
方法在受影响的数据库上获得一个写锁,并将阻塞其他操作,直到它完成。
I think using the db.remove({})
method is better than db.drop()
.
我认为使用该db.remove({})
方法比db.drop()
.