2019-07-11 11:28:07 -04:00
|
|
|
// Test that queryExecStats within a $collStats stage returns the correct execution stats.
|
2020-08-18 16:16:48 -04:00
|
|
|
// @tags: [
|
|
|
|
|
// assumes_no_implicit_collection_creation_after_drop,
|
2022-06-02 11:50:14 +00:00
|
|
|
// does_not_support_repeated_reads,
|
2020-08-18 16:16:48 -04:00
|
|
|
// ]
|
2019-07-11 11:28:07 -04:00
|
|
|
import {assertErrorCode} from "jstests/aggregation/extras/utils.js";
|
|
|
|
|
|
|
|
|
|
const nDocs = 32;
|
|
|
|
|
|
|
|
|
|
const testDB = db.getSiblingDB("aggregation_query_exec_stats");
|
|
|
|
|
const coll = testDB.aggregation_query_exec_stats;
|
|
|
|
|
coll.drop();
|
2025-08-21 10:17:44 -07:00
|
|
|
assert.commandWorked(testDB.createCollection("aggregation_query_exec_stats", {capped: true, size: nDocs * 100}));
|
2019-07-11 11:28:07 -04:00
|
|
|
|
|
|
|
|
for (let i = 0; i < nDocs; i++) {
|
|
|
|
|
assert.commandWorked(coll.insert({a: i}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Run a bunch of collection scans on the server.
|
|
|
|
|
for (let i = 0; i < nDocs; i++) {
|
|
|
|
|
assert.eq(coll.find({a: i}).itcount(), 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Test that an error is returned if queryExecStats is not an object.
|
|
|
|
|
let pipeline = [{$collStats: {queryExecStats: 1}}];
|
2021-03-02 16:04:42 -05:00
|
|
|
assertErrorCode(coll, pipeline, ErrorCodes.TypeMismatch, "queryExecStats spec must be an object");
|
2019-07-11 11:28:07 -04:00
|
|
|
pipeline = [{$collStats: {queryExecStats: "1"}}];
|
2021-03-02 16:04:42 -05:00
|
|
|
assertErrorCode(coll, pipeline, ErrorCodes.TypeMismatch, "queryExecStats spec must be an object");
|
2019-07-11 11:28:07 -04:00
|
|
|
|
|
|
|
|
// Test the accuracy of the result of queryExecStats as a standalone option.
|
|
|
|
|
pipeline = [{$collStats: {queryExecStats: {}}}];
|
|
|
|
|
let result = coll.aggregate(pipeline).next();
|
|
|
|
|
assert.eq(nDocs, result.queryExecStats.collectionScans.total);
|
|
|
|
|
assert.eq(nDocs, result.queryExecStats.collectionScans.nonTailable);
|
|
|
|
|
|
|
|
|
|
// Test tailable collection scans update collectionScans counters appropriately.
|
|
|
|
|
for (let i = 0; i < nDocs; i++) {
|
|
|
|
|
assert.eq(coll.find({a: i}).tailable().itcount(), 1);
|
|
|
|
|
}
|
|
|
|
|
result = coll.aggregate(pipeline).next();
|
|
|
|
|
assert.eq(nDocs * 2, result.queryExecStats.collectionScans.total);
|
|
|
|
|
assert.eq(nDocs, result.queryExecStats.collectionScans.nonTailable);
|
|
|
|
|
|
|
|
|
|
// Run a query which will require the client to fetch multiple batches from the server. Ensure
|
|
|
|
|
// that the getMore commands don't increment the counter of collection scans.
|
|
|
|
|
assert.eq(coll.find({}).batchSize(2).itcount(), nDocs);
|
|
|
|
|
result = coll.aggregate(pipeline).next();
|
2025-08-21 10:17:44 -07:00
|
|
|
assert.eq(nDocs * 2 + 1, result.queryExecStats.collectionScans.total);
|
2019-07-11 11:28:07 -04:00
|
|
|
assert.eq(nDocs + 1, result.queryExecStats.collectionScans.nonTailable);
|
|
|
|
|
|
|
|
|
|
// Create index to test that index scans don't up the collection scan counter.
|
|
|
|
|
assert.commandWorked(coll.createIndex({a: 1}));
|
|
|
|
|
// Run a bunch of index scans.
|
|
|
|
|
for (let i = 0; i < nDocs; i++) {
|
|
|
|
|
assert.eq(coll.find({a: i}).itcount(), 1);
|
|
|
|
|
}
|
|
|
|
|
result = coll.aggregate(pipeline).next();
|
|
|
|
|
// Assert that the number of collection scans hasn't increased.
|
2025-08-21 10:17:44 -07:00
|
|
|
assert.eq(nDocs * 2 + 1, result.queryExecStats.collectionScans.total);
|
2019-07-11 11:28:07 -04:00
|
|
|
assert.eq(nDocs + 1, result.queryExecStats.collectionScans.nonTailable);
|
|
|
|
|
|
|
|
|
|
// Test that we error when the collection does not exist.
|
|
|
|
|
coll.drop();
|
|
|
|
|
pipeline = [{$collStats: {queryExecStats: {}}}];
|
2020-12-07 17:05:44 +00:00
|
|
|
assertErrorCode(coll, pipeline, ErrorCodes.NamespaceNotFound);
|
2019-07-11 11:28:07 -04:00
|
|
|
|
|
|
|
|
// Test that we error when the database does not exist.
|
|
|
|
|
assert.commandWorked(testDB.dropDatabase());
|
2025-08-21 10:17:44 -07:00
|
|
|
assertErrorCode(coll, pipeline, ErrorCodes.NamespaceNotFound);
|