首页 > 解决方案 > mongodb - 使用 mongodump 和 mongorestore 后数据丢失?

问题描述

我注意到使用 mongodump 和 mongorestore 备份和恢复数据库后,我的 mongodb 的大小变小了:

> show dbs
iotdb1  0.611GB
iotdb2  0.476GB

原始大小为 0.611GB,但现在为 0.476GB。

这是正常的吗?

标签: mongodbmongodumpmongorestore

解决方案


简短的回答:

转储和恢复时,数据库大小无关紧要。文件只算重要。

解释:

Mongodump 不会备份,oplog因此您可能会看到一些空间差异。此外,当您进行转储时data holes or bloat,系统中已经存在的一些也会在您恢复它们时被删除。这是您在还原数据库后感觉数据库大小发生变化的两个主要原因。

确认:

您可以使用以下命令在两个数据库上查找之前和之后的所有文档大小的列表:

db.getCollectionNames().forEach(function(collection) { resultCount = db[collection].count(); print("" + collection + "的文档数: "+ resultCount); });

样本输出:

> db.getCollectionNames().forEach(function(collection) { resultCount = db[collection].count(); print("Number of documents for " + collection + ": "+ resultCount); });
Number of documents for credits: 17643
Number of documents for data: 500302
Number of documents for debts: 387
Number of documents for food: 309875
Number of documents for loans: 64926
Number of documents for payroll: 2436
Number of documents for tasks: 11564
Number of documents for salary: 883457
Number of documents for savings: 494

因此,通过上述查询,您可以轻松比较两个数据库并了解文档编号匹配。


推荐阅读