mongo/jstests/core/query/queryoptimizera.js
Steve McClure 1ffbc6c2e9 SERVER-109432: Autofix JS var usage to favor let (#40637)
GitOrigin-RevId: 9674b7db36a0f3f650d39c1e3fb2ad6ff2141cfb
2025-08-28 19:21:01 +00:00

99 lines
2.7 KiB
JavaScript

// Check that a warning message about doing a capped collection scan for a query with an _id
// constraint is printed at appropriate times. SERVER-5353
//
// @tags: [
// # The test runs commands that are not allowed with security token: getLog.
// not_allowed_with_signed_security_token,
// does_not_support_stepdowns,
// requires_capped,
// requires_getmore,
// ]
function numWarnings() {
let logs = db.adminCommand({getLog: "global"}).log;
let ret = 0;
logs.forEach(function (x) {
if (x.match(warningMatchRegexp)) {
++ret;
}
});
return ret;
}
let collectionNameIndex = 0;
// Generate a collection name not already present in the log.
do {
var testCollectionName = "jstests_queryoptimizera__" + collectionNameIndex++;
let warningMatchString = "unindexed _id query on capped collection.*collection: test." + testCollectionName;
var warningMatchRegexp = new RegExp(warningMatchString);
} while (numWarnings() > 0);
let t = db[testCollectionName];
t.drop();
let notCappedCollectionName = testCollectionName + "_notCapped";
let notCapped = db.getSiblingDB("local").getCollection(notCappedCollectionName);
notCapped.drop();
assert.commandWorked(db.createCollection(testCollectionName, {capped: true, size: 1000}));
assert.commandWorked(notCapped.getDB().createCollection(notCappedCollectionName));
t.insert({});
notCapped.insert({});
let oldNumWarnings = 0;
function assertNoNewWarnings() {
assert.eq(oldNumWarnings, numWarnings());
}
function assertNewWarning() {
let newNumWarnings = numWarnings();
// Ensure that newNumWarnings > oldNumWarnings. It's not safe to test that oldNumWarnings + 1
// == newNumWarnings, because a (simulated) page fault exception may cause multiple messages to
// be logged instead of only one.
assert.lt(oldNumWarnings, newNumWarnings);
oldNumWarnings = newNumWarnings;
}
// Simple _id query
t.find({_id: 0}).itcount();
assertNoNewWarnings();
// Simple _id query without an _id index, on a non capped collection.
notCapped.find({_id: 0}).itcount();
assertNoNewWarnings();
// A multi field query, including _id.
t.find({_id: 0, a: 0}).itcount();
assertNoNewWarnings();
// An unsatisfiable query.
t.find({_id: 0, a: {$in: []}}).itcount();
assertNoNewWarnings();
// An hinted query.
t.find({_id: 0}).hint({$natural: 1}).itcount();
assertNoNewWarnings();
// Retry a multi field query.
t.find({_id: 0, a: 0}).itcount();
assertNoNewWarnings();
// Warnings should not be printed when an index is added on _id.
t.createIndex({_id: 1});
t.find({_id: 0}).itcount();
assertNoNewWarnings();
t.find({_id: 0, a: 0}).itcount();
assertNoNewWarnings();
t.find({_id: 0, a: 0}).itcount();
assertNoNewWarnings();
t.drop(); // cleanup
notCapped.drop();