diff options
author | Nick Zolnierz <nicholas.zolnierz@mongodb.com> | 2017-06-14 14:34:39 -0400 |
---|---|---|
committer | Nick Zolnierz <nicholas.zolnierz@mongodb.com> | 2017-06-15 13:44:22 -0400 |
commit | 1efbf03521e9c6d85df33ab6786f6c121d8b56c2 (patch) | |
tree | 43d538d303780a70aa7c532475071ba71554b03a /src/mongo/db/stats | |
parent | 96d35cfa5c3212124e0ffdbdcb81a26565b022e3 (diff) | |
download | mongo-1efbf03521e9c6d85df33ab6786f6c121d8b56c2.tar.gz |
SERVER-29620 Add shorthand syntax for retrieving the record count for a collection using the agg stage
Diffstat (limited to 'src/mongo/db/stats')
-rw-r--r-- | src/mongo/db/stats/storage_stats.cpp | 20 | ||||
-rw-r--r-- | src/mongo/db/stats/storage_stats.h | 9 |
2 files changed, 28 insertions, 1 deletions
diff --git a/src/mongo/db/stats/storage_stats.cpp b/src/mongo/db/stats/storage_stats.cpp index fca73b4a473..12c41f93fb0 100644 --- a/src/mongo/db/stats/storage_stats.cpp +++ b/src/mongo/db/stats/storage_stats.cpp @@ -108,4 +108,24 @@ Status appendCollectionStorageStats(OperationContext* opCtx, return Status::OK(); } + +Status appendCollectionRecordCount(OperationContext* opCtx, + const NamespaceString& nss, + BSONObjBuilder* result) { + AutoGetCollectionForReadCommand ctx(opCtx, nss); + if (!ctx.getDb()) { + return {ErrorCodes::BadValue, + str::stream() << "Database [" << nss.db().toString() << "] not found."}; + } + + Collection* collection = ctx.getCollection(); + if (!collection) { + return {ErrorCodes::BadValue, + str::stream() << "Collection [" << nss.toString() << "] not found."}; + } + + result->appendNumber("count", collection->numRecords(opCtx)); + + return Status::OK(); +} } // namespace mongo diff --git a/src/mongo/db/stats/storage_stats.h b/src/mongo/db/stats/storage_stats.h index 0f62229c80a..f3a3db99b9b 100644 --- a/src/mongo/db/stats/storage_stats.h +++ b/src/mongo/db/stats/storage_stats.h @@ -38,10 +38,17 @@ namespace mongo { /** * Appends to 'builder' storage related statistics for the collection represented by 'nss'. - * Used by both the collStats command and the $collStats aggregation stage. */ Status appendCollectionStorageStats(OperationContext* opCtx, const NamespaceString& nss, const BSONObj& param, BSONObjBuilder* builder); + +/** + * Appends the collection record count to 'builder' for the collection represented by 'nss'. + */ +Status appendCollectionRecordCount(OperationContext* opCtx, + const NamespaceString& nss, + BSONObjBuilder* builder); + }; // namespace mongo |