diff options
-rw-r--r-- | jstests/core/dbstats.js | 13 | ||||
-rw-r--r-- | src/mongo/db/catalog/database_impl.cpp | 2 | ||||
-rw-r--r-- | src/mongo/db/commands/dbcommands.cpp | 26 |
3 files changed, 32 insertions, 9 deletions
diff --git a/jstests/core/dbstats.js b/jstests/core/dbstats.js index a7cbd9c6bb8..559b6c11774 100644 --- a/jstests/core/dbstats.js +++ b/jstests/core/dbstats.js @@ -84,4 +84,17 @@ if (!isMongoS) { assert.eq(2, dbStats.collections, tojson(dbStats)); // testColl + system.views assert.eq(1, dbStats.views, tojson(dbStats)); } + +// Check that the output for non-existing database and the output for empty database +// have the same fields. +const testEmptyAndNonExistingDB = db.getSiblingDB(jsTestName() + "_non_existing_and_empty"); +testEmptyAndNonExistingDB.dropDatabase(); + +const statsNonExistingDB = testEmptyAndNonExistingDB.runCommand({dbStats: 1, freeStorage: 1}); +testEmptyAndNonExistingDB.runCommand({create: "test_empty_collection"}); + +const statsEmptyDB = testEmptyAndNonExistingDB.runCommand({dbStats: 1, freeStorage: 1}); +assert.sameMembers(Object.keys(statsNonExistingDB), + Object.keys(statsEmptyDB), + "dbStats for non-existing and empty dbs should return the same fields"); })(); diff --git a/src/mongo/db/catalog/database_impl.cpp b/src/mongo/db/catalog/database_impl.cpp index 5e092f5d5b1..c46c8f76235 100644 --- a/src/mongo/db/catalog/database_impl.cpp +++ b/src/mongo/db/catalog/database_impl.cpp @@ -369,6 +369,8 @@ void DatabaseImpl::getStats(OperationContext* opCtx, return true; }); + // Make sure that the same fields are returned for non-existing dbs + // in `DBStats::errmsgRun` output->appendNumber("collections", nCollections); output->appendNumber("views", nViews); output->appendNumber("objects", objects); diff --git a/src/mongo/db/commands/dbcommands.cpp b/src/mongo/db/commands/dbcommands.cpp index 4a2caafb16d..2267c619692 100644 --- a/src/mongo/db/commands/dbcommands.cpp +++ b/src/mongo/db/commands/dbcommands.cpp @@ -663,22 +663,30 @@ public: Database* db = autoDb.getDb(); if (!db) { - // TODO: This preserves old behaviour where we used to create an empty database - // metadata even when the database is accessed for read. Without this several - // unit-tests will fail, which are fairly easy to fix. If backwards compatibility - // is not needed for the missing DB case, we can just do the same that's done in - // CollectionStats. + // This preserves old behavior where we used to create an empty database even when the + // database was accessed for a read. Changing this behavior would impact users that have + // learned to depend on it, so we continue to support it. Ensure that these fields match + // exactly the fields in `DatabaseImpl::getStats`. + result.appendNumber("collections", 0); result.appendNumber("views", 0); result.appendNumber("objects", 0); result.append("avgObjSize", 0); result.appendNumber("dataSize", 0); result.appendNumber("storageSize", 0); - result.appendNumber("totalSize", 0); - result.appendNumber("indexes", 0); - result.appendNumber("indexSize", 0); + if (includeFreeStorage) { + result.appendNumber("freeStorageSize", 0); + result.appendNumber("indexes", 0); + result.appendNumber("indexSize", 0); + result.appendNumber("indexFreeStorageSize", 0); + result.appendNumber("totalSize", 0); + result.appendNumber("totalFreeStorageSize", 0); + } else { + result.appendNumber("indexes", 0); + result.appendNumber("indexSize", 0); + result.appendNumber("totalSize", 0); + } result.appendNumber("scaleFactor", scale); - result.appendNumber("fileSize", 0); if (!getGlobalServiceContext()->getStorageEngine()->isEphemeral()) { result.appendNumber("fsUsedSize", 0); result.appendNumber("fsTotalSize", 0); |