From 1fad834fc03115eb797bd32ffae144536f589275 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BF=B7=E5=AD=90=20=28Maiko=20Tan=29?= Date: Tue, 5 Apr 2022 16:47:56 +0800 Subject: [PATCH] fix(mongo): db.totalSize can be undefined, fix #568 (#592) --- plugins/database/mongo/src/index.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/plugins/database/mongo/src/index.ts b/plugins/database/mongo/src/index.ts index c57d5d3ad0..87bd2be129 100644 --- a/plugins/database/mongo/src/index.ts +++ b/plugins/database/mongo/src/index.ts @@ -119,10 +119,14 @@ class MongoDatabase extends Database { async stats() { // https://docs.mongodb.com/manual/reference/command/dbStats/#std-label-dbstats-output - const [{ totalSize }, tables] = await Promise.all([ + const [stats, tables] = await Promise.all([ this.db.stats(), this._collStats(), ]) + // While MongoDB's document above says that the `stats.totalSize` is the sum of + // `stats.dataSize` and `stats.storageSize`, it's actually `undefined` in some cases. + // So we have to calculate it manually. + const totalSize = stats.indexSize + stats.storageSize return { size: totalSize, tables } }