feat(mongo): improve query suggestions for nested operators and options

This commit is contained in:
Loc Nguyen authored and GitHub committed 2026-10-06 21:28:26 +08:00
1 parent 0503d3b9d1
commit c6159213aa
3 files changed
+423 -42

No files matched your search

+171 -1
View File
@@ -237,6 +237,23 @@ test("recognises toArray and pretty in find chains and keeps count restricted",
assert.deepEqual(labels("db.characters.find({}).sort({ name: 1 }).toArray()."), expectedChainMethods);
});
test("matches cursor chains across nested parentheses in arguments", () => {
const expectedChainMethods = ["limit", "sort", "skip", "explain", "collation", "toArray", "pretty"];
assert.deepEqual(labels("db.characters.find({}).sort({ x: Math.max(1, 2) })."), expectedChainMethods);
assert.deepEqual(labels("db.characters.find({ ratio: Math.min(a, b) }).limit(Math.max(1, 5))."), expectedChainMethods);
});
test("keeps cursor method completion anchored to a dot in the chain", () => {
// A completed call without a trailing dot is not a method-dot position.
assert.equal(labels("db.characters.find()").includes("limit"), false);
assert.equal(labels("db.characters.find({});").includes("limit"), false);
assert.equal(labels("db.characters.find() show col").includes("limit"), false);
});
test("completes field refs inside compound $group _id arrays", () => {
assert.ok(labels('db.users.aggregate([{ $group: { _id: [ "$', { fields }).includes("$name"));
});
test("stops offering root snippets after terminal cursor methods count and explain", () => {
// Nothing can follow count() or explain(); these positions must yield mode "none" rather than root db.* snippets.
assert.equal(getMongoCompletionContext("db.users.find({}).count().", "db.users.find({}).count().".length).mode, "none");
@@ -426,7 +443,7 @@ test("offers whole-filter operators at the top level and field operators under a
assert.ok(under.includes("$oid"));
assert.equal(under.includes("$or"), false, "$or is not valid under a field");
assert.equal(under.includes("$and"), false);
assert.deepEqual(labels("db.users.find({ _id: { $o", { fields }), ["$oid"]);
assert.deepEqual(labels("db.users.find({ _id: { $o", { fields }), ["$options", "$oid"]);
// Fields lead when nothing has been typed yet.
const bare = labels("db.users.find({ ", { fields });
@@ -1537,6 +1554,24 @@ test("every suggested sub-document key and enumerated value parses in the positi
clusteredIndex: (body) => `db.createCollection("x", { clusteredIndex: { ${body} } })`,
range: (body) => `db.users.aggregate([{ $densify: { field: "t", range: { ${body} } } }])`,
fillOutput: (body) => `db.users.aggregate([{ $fill: { output: { score: { ${body} } } } }])`,
roles: (body) => `db.createUser({ user: "x", pwd: "y", roles: [ { ${body} } ] })`,
$dateToString: (body) => `db.users.aggregate([{ $project: { d: { $dateToString: { ${body} } } } }])`,
$dateFromParts: (body) => `db.users.aggregate([{ $project: { d: { $dateFromParts: { ${body} } } } }])`,
$dateToParts: (body) => `db.users.aggregate([{ $project: { d: { $dateToParts: { ${body} } } } }])`,
$trim: (body) => `db.users.aggregate([{ $project: { d: { $trim: { ${body} } } } }])`,
$ltrim: (body) => `db.users.aggregate([{ $project: { d: { $ltrim: { ${body} } } } }])`,
$rtrim: (body) => `db.users.aggregate([{ $project: { d: { $rtrim: { ${body} } } } }])`,
$replaceOne: (body) => `db.users.aggregate([{ $project: { d: { $replaceOne: { ${body} } } } }])`,
$replaceAll: (body) => `db.users.aggregate([{ $project: { d: { $replaceAll: { ${body} } } } }])`,
$regexMatch: (body) => `db.users.aggregate([{ $project: { d: { $regexMatch: { ${body} } } } }])`,
$regexFind: (body) => `db.users.aggregate([{ $project: { d: { $regexFind: { ${body} } } } }])`,
$regexFindAll: (body) => `db.users.aggregate([{ $project: { d: { $regexFindAll: { ${body} } } } }])`,
$filter: (body) => `db.users.aggregate([{ $project: { d: { $filter: { ${body} } } } }])`,
$map: (body) => `db.users.aggregate([{ $project: { d: { $map: { ${body} } } } }])`,
$reduce: (body) => `db.users.aggregate([{ $project: { d: { $reduce: { ${body} } } } }])`,
$cond: (body) => `db.users.aggregate([{ $project: { d: { $cond: { ${body} } } } }])`,
$switch: (body) => `db.users.aggregate([{ $project: { d: { $switch: { ${body} } } } }])`,
switchBranch: (body) => `db.users.aggregate([{ $project: { d: { $switch: { branches: [ { ${body} } ] } } } }])`,
};
for (const [operator, keys] of Object.entries(OPERATOR_SUB_KEYS)) {
const build = keyCommands[operator];
@@ -1572,6 +1607,7 @@ test("every suggested sub-document key and enumerated value parses in the positi
locale: (value) => `db.users.find({}).collation({ locale: ${value} })`,
whenMatched: (value) => `db.users.aggregate([{ $merge: { into: "out", whenMatched: ${value} } }])`,
whenNotMatched: (value) => `db.users.aggregate([{ $merge: { into: "out", whenNotMatched: ${value} } }])`,
builtInRole: (value) => `db.createUser({ user: "x", pwd: "y", roles: [ { role: ${value} } ] })`,
};
for (const [enumKey, values] of Object.entries(ENUM_VALUES)) {
const build = valueCommands[enumKey];
@@ -2415,3 +2451,137 @@ test("preserves auto-trigger and completion for getCollection and getSiblingDB d
assert.equal(getMongoCompletionContext(siblingGetCollectionDot, siblingGetCollectionDot.length).mode, "method");
assert.equal(shouldAutoOpenMongoCompletion(siblingGetCollectionDot, siblingGetCollectionDot.length), true);
});
test("completes expression operator sub-document keys and enum values", () => {
// Sub-keys for $dateToString
const dateToStringKeys = labels("db.users.aggregate([{ $project: { d: { $dateToString: { ");
assert.ok(dateToStringKeys.includes("date"));
assert.ok(dateToStringKeys.includes("format"));
assert.ok(dateToStringKeys.includes("timezone"));
assert.ok(dateToStringKeys.includes("onNull"));
// Sub-keys for $dateFromParts
const dateFromPartsKeys = labels("db.users.aggregate([{ $project: { d: { $dateFromParts: { ");
assert.ok(dateFromPartsKeys.includes("year"));
assert.ok(dateFromPartsKeys.includes("month"));
assert.ok(dateFromPartsKeys.includes("day"));
assert.ok(dateFromPartsKeys.includes("timezone"));
// Sub-keys for $filter
const filterKeys = labels("db.users.aggregate([{ $project: { d: { $filter: { ");
assert.ok(filterKeys.includes("input"));
assert.ok(filterKeys.includes("as"));
assert.ok(filterKeys.includes("cond"));
assert.ok(filterKeys.includes("limit"));
// Sub-keys for $cond
const condKeys = labels("db.users.aggregate([{ $project: { d: { $cond: { ");
assert.ok(condKeys.includes("if"));
assert.ok(condKeys.includes("then"));
assert.ok(condKeys.includes("else"));
// Sub-keys for $switch
const switchKeys = labels("db.users.aggregate([{ $project: { d: { $switch: { ");
assert.ok(switchKeys.includes("branches"));
assert.ok(switchKeys.includes("default"));
// Sub-keys for $regexMatch
const regexMatchKeys = labels("db.users.aggregate([{ $project: { d: { $regexMatch: { ");
assert.ok(regexMatchKeys.includes("input"));
assert.ok(regexMatchKeys.includes("regex"));
assert.ok(regexMatchKeys.includes("options"));
// Enum values for options ($regexMatch, $regexFind, $regexFindAll)
const regexOptions = labels("db.users.aggregate([{ $project: { d: { $regexMatch: { options: ");
assert.ok(regexOptions.includes("i"));
assert.ok(regexOptions.includes("m"));
assert.ok(regexOptions.includes("x"));
assert.ok(regexOptions.includes("s"));
assert.ok(regexOptions.includes("u"));
const regexOptionsQuoted = labels('db.users.aggregate([{ $project: { d: { $regexMatch: { options: "');
assert.ok(regexOptionsQuoted.includes("i"));
// Enum values for iso8601 ($dateToParts)
const dateToPartsIso = labels("db.users.aggregate([{ $project: { d: { $dateToParts: { iso8601: ");
assert.ok(dateToPartsIso.includes("true"));
assert.ok(dateToPartsIso.includes("false"));
// Used key deduplication inside operator sub-documents
const dedupedKeys = labels("db.users.aggregate([{ $project: { d: { $dateToString: { date: '$created', ");
assert.equal(dedupedKeys.includes("date"), false, "already specified key must not be re-suggested");
assert.ok(dedupedKeys.includes("format"));
assert.ok(dedupedKeys.includes("timezone"));
assert.ok(dedupedKeys.includes("onNull"));
});
test("completes $switch branches array elements and fields", () => {
// Empty branch object inside branches array suggests switchBranch keys: case and then
const branchKeys = labels("db.users.aggregate([{ $project: { d: { $switch: { branches: [ { ");
assert.ok(branchKeys.includes("case"));
assert.ok(branchKeys.includes("then"));
// After specifying case, used key deduplication leaves then
const branchAfterCase = labels("db.users.aggregate([{ $project: { d: { $switch: { branches: [ { case: 1, ");
assert.equal(branchAfterCase.includes("case"), false);
assert.ok(branchAfterCase.includes("then"));
// Context mode verification
const branchCtx = getMongoCompletionContext("db.users.aggregate([{ $project: { d: { $switch: { branches: [ { ", "db.users.aggregate([{ $project: { d: { $switch: { branches: [ { ".length);
assert.equal(branchCtx.mode, "operatorField");
assert.equal(branchCtx.operator, "switchBranch");
// In value position of case:, fieldRef mode is used
const caseValCtx = getMongoCompletionContext("db.users.aggregate([{ $project: { d: { $switch: { branches: [ { case: ", "db.users.aggregate([{ $project: { d: { $switch: { branches: [ { case: ".length);
assert.equal(caseValCtx.mode, "fieldRef");
});
test("completes expression operator sub-keys and enums inside $expr in find() and $match", () => {
// find() with $expr -> $dateToString keys
const findExprDate = labels("db.users.find({ $expr: { $dateToString: { ");
assert.ok(findExprDate.includes("date"));
assert.ok(findExprDate.includes("format"));
assert.ok(findExprDate.includes("timezone"));
assert.ok(findExprDate.includes("onNull"));
// find() with $expr -> $regexMatch options enum
const findExprRegexOptions = labels("db.users.find({ $expr: { $regexMatch: { options: ");
assert.ok(findExprRegexOptions.includes("i"));
assert.ok(findExprRegexOptions.includes("m"));
// find() with $expr -> $dateToParts iso8601 enum
const findExprDatePartsIso = labels("db.users.find({ $expr: { $dateToParts: { iso8601: ");
assert.ok(findExprDatePartsIso.includes("true"));
assert.ok(findExprDatePartsIso.includes("false"));
// find() with $expr -> $switch branches
const findExprSwitchBranch = labels("db.users.find({ $expr: { $switch: { branches: [ { ");
assert.ok(findExprSwitchBranch.includes("case"));
assert.ok(findExprSwitchBranch.includes("then"));
// $match with $expr -> $dateToString keys
const matchExprDate = labels("db.users.aggregate([{ $match: { $expr: { $dateToString: { ");
assert.ok(matchExprDate.includes("date"));
assert.ok(matchExprDate.includes("format"));
// $match with $expr -> $regexMatch options enum
const matchExprRegexOptions = labels("db.users.aggregate([{ $match: { $expr: { $regexMatch: { options: ");
assert.ok(matchExprRegexOptions.includes("i"));
assert.ok(matchExprRegexOptions.includes("m"));
});
test("completes expression operator sub-keys and enums in $group", () => {
// $group _id compound key with $dateToString
const groupIdDate = labels("db.users.aggregate([{ $group: { _id: { day: { $dateToString: { ");
assert.ok(groupIdDate.includes("date"));
assert.ok(groupIdDate.includes("format"));
// $group accumulator with expression sub-keys and enums
const groupAccumDate = labels("db.users.aggregate([{ $group: { total: { $sum: { $dateToString: { ");
assert.ok(groupAccumDate.includes("date"));
assert.ok(groupAccumDate.includes("format"));
const groupAccumRegexOpt = labels("db.users.aggregate([{ $group: { total: { $sum: { $regexMatch: { options: ");
assert.ok(groupAccumRegexOpt.includes("i"));
assert.ok(groupAccumRegexOpt.includes("m"));
});