feat(mongodb): support estimatedDocumentCount, db.stats, db.serverStatus

This commit is contained in:
Loc Nguyen authored and GitHub committed 2026-09-16 00:40:32 +08:00
1 parent 7f12fecc08
commit 69b138dc22
5 files changed
+140 -8

No files matched your search

+12 -3
View File
@@ -134,8 +134,8 @@ test("prioritizes common read helpers and keeps destructive helpers last", () =>
const getCollectionMethodLabels = labels('db.getCollection("order-events").');
assert.deepEqual(labels("").slice(0, 5), ["db.collection.find", "db.collection.aggregate", "db.getCollection", "use", "db.version"]);
assert.deepEqual(methodLabels.slice(0, 5), ["find", "findOne", "aggregate", "countDocuments", "distinct"]);
assert.deepEqual(getCollectionMethodLabels.slice(0, 5), ["find", "findOne", "aggregate", "countDocuments", "distinct"]);
assert.deepEqual(methodLabels.slice(0, 6), ["find", "findOne", "aggregate", "countDocuments", "estimatedDocumentCount", "distinct"]);
assert.deepEqual(getCollectionMethodLabels.slice(0, 6), ["find", "findOne", "aggregate", "countDocuments", "estimatedDocumentCount", "distinct"]);
assert.deepEqual(methodLabels.slice(-3), ["dropIndex", "dropIndexes", "drop"]);
assert.deepEqual(labels("db.users.find({})."), ["limit", "sort", "skip", "count"]);
});
@@ -248,6 +248,14 @@ test("treats $in, $nin and $all elements as values rather than sub-filters", ()
assert.equal(labels("db.users.find({ $or: [{ ").includes("$oid"), false);
});
test("offers the newly supported count and database commands", () => {
assert.ok(labels("db.users.estim", { fields }).includes("estimatedDocumentCount"));
const dbLevel = labels("db.");
assert.ok(dbLevel.includes("stats"));
assert.ok(dbLevel.includes("serverStatus"));
assert.ok(labels("").includes("db.stats"));
});
test("offers the newer shell value constructors", () => {
const labels = buildMongoCompletionItems("db.users.find({ _id: ", "db.users.find({ _id: ".length, { fields }).map((item) => item.label);
for (const constructor of ["NumberInt", "NumberDecimal", "UUID", "BinData", "Timestamp", "MinKey", "MaxKey"]) {
@@ -491,8 +499,9 @@ test("suggests only helpers the shell parser accepts", () => {
assert.ok(methodLabels.includes("count"));
assert.ok(methodLabels.includes("drop"));
assert.ok(methodLabels.includes("distinct"));
assert.ok(methodLabels.includes("estimatedDocumentCount"));
// Suggesting a helper DBX cannot run just hands the user a command that fails.
for (const unsupported of ["bulkWrite", "estimatedDocumentCount", "replaceOne"]) {
for (const unsupported of ["bulkWrite", "replaceOne", "renameCollection"]) {
assert.equal(methodLabels.includes(unsupported), false, `${unsupported} is not executable`);
}
// Cursor methods are not collection methods.
+30 -1
View File
@@ -1161,6 +1161,35 @@ test("parseMongoAggregateCommand rejects non-array pipelines and invalid options
assert.equal(parseMongoAggregateCommand("db.products.aggregate([], {}, true)"), null);
});
test("parseMongoCountDocumentsCommand reads estimatedDocumentCount as a metadata-backed count", () => {
// The driver already takes the metadata fast path for a filterless legacy count().
const expected = { collection: "orders", filter: "{}", mode: "legacy" };
for (const source of ["db.orders.estimatedDocumentCount()", 'db["orders"].estimatedDocumentCount()', "db.getCollection('orders').estimatedDocumentCount();"]) {
assert.deepEqual(parseMongoCountDocumentsCommand(source), expected, source);
}
assert.equal(parseMongoCountDocumentsCommand("db.orders.estimatedDocumentCount({a: 1})"), null);
assert.equal(parseMongoCountDocumentsCommand("db.orders.estimatedDocumentCount().limit(5)"), null);
// count() and countDocuments() keep their own modes.
assert.equal(parseMongoCountDocumentsCommand("db.orders.countDocuments({})")?.mode, "accurate");
assert.equal(parseMongoCountDocumentsCommand("db.orders.count()")?.mode, "legacy");
});
test("parseMongoRunCommand reads db.stats() and db.serverStatus() as run commands", () => {
assert.deepEqual(parseMongoRunCommand("db.stats()"), { commandJson: '{"dbStats":1}' });
assert.deepEqual(parseMongoRunCommand("db . serverStatus ( ) ;"), { commandJson: '{"serverStatus":1}' });
assert.deepEqual(parseMongoRunCommand("DB.STATS()"), { commandJson: '{"dbStats":1}' });
for (const source of ["db.stats(1)", "db.serverStatus({})"]) {
assert.equal(parseMongoRunCommand(source), null, source);
assert.match(describeMongoCommandParseFailure(source), /takes no arguments|expects no arguments/, source);
}
// db.collection.stats() is still collection stats, not a run command.
assert.equal(parseMongoRunCommand("db.orders.stats()"), null);
assert.equal(parseMongoCommand("db.orders.stats()")?.command.kind, "collectionStats");
});
test("describeMongoCommandParseFailure names an unsupported value constructor and where it is", () => {
const message = describeMongoCommandParseFailure('db.reports.updateOne({a: 1}, {$set: {t: Foo("x")}}, {upsert: true})');
assert.match(message, /Unsupported value Foo\(\.\.\.\) in the update argument of updateOne\(\)/);
@@ -1187,7 +1216,7 @@ test("describeMongoCommandParseFailure names unsupported methods and points at a
assert.match(describeMongoCommandParseFailure('db["my-coll"].renameCollection("x")'), /renameCollection\(\) is not supported/);
assert.match(describeMongoCommandParseFailure('db.getCollection("my-coll").watch()'), /watch\(\) is not supported/);
assert.match(describeMongoCommandParseFailure("db.stats()"), /db\.stats\(\) is not supported; use db\.runCommand\(\{ dbStats: 1 \}\)/);
assert.match(describeMongoCommandParseFailure('db.createCollection("c")'), /db\.createCollection\(\) is not supported; collections are created on first insert/);
assert.match(describeMongoCommandParseFailure('db.getSiblingDB("other").c.find({})'), /use <database>/);
assert.match(describeMongoCommandParseFailure("db.adminCommand({ping: 1})"), /use db\.runCommand/);
assert.match(describeMongoCommandParseFailure("show collections"), /listed in the sidebar/);