fix(mongodb): complete $elemMatch with the operators it accepts

This commit is contained in:
Loc Nguyen authored and GitHub committed 2026-10-04 16:45:13 +08:00
1 parent b66d7313f1
commit ee2573f7db
3 files changed
+51 -5

No files matched your search

@@ -6,6 +6,7 @@ import {
ENUM_VALUES,
EXTENDED_JSON_VALUES,
FIELD_QUERY_OPERATORS,
ELEM_MATCH_QUERY_OPERATORS,
PIPELINE_STAGES,
PUSH_MODIFIERS,
BULK_WRITE_OPERATION_FIELDS,
@@ -45,6 +46,7 @@ export type MongoCompletionMode =
| "field"
| "filterField"
| "pullCondition"
| "elemMatchKey"
| "fieldPath"
| "fieldRef"
| "indexName"
@@ -496,6 +498,10 @@ export function buildMongoCompletionItemsFromContext(context: MongoCompletionCon
// Fields lead; query operators follow once `$` is typed.
items = [...fieldItems(prefix, fields), ...specItems(FIELD_QUERY_OPERATORS, prefix, "query operator", 80)];
break;
case "elemMatchKey":
// Fields lead; field query operators and $and / $or / $nor follow once `$` is typed.
items = [...fieldItems(prefix, fields), ...specItems(ELEM_MATCH_QUERY_OPERATORS, prefix, "query operator", 80)];
break;
case "fieldPath":
items = fieldPathItems(prefix, fields);
break;
@@ -569,7 +575,7 @@ export function buildMongoCompletionItemsFromContext(context: MongoCompletionCon
/** Modes whose items are built from the target collection's sampled fields. */
export function mongoCompletionNeedsFields(mode: MongoCompletionMode): boolean {
return mode === "field" || mode === "filterField" || mode === "pullCondition" || mode === "fieldPath" || mode === "fieldRef" || mode === "expression";
return mode === "field" || mode === "filterField" || mode === "pullCondition" || mode === "elemMatchKey" || mode === "fieldPath" || mode === "fieldRef" || mode === "expression";
}
/** Modes whose items are built from the database's collection names. */
@@ -1049,6 +1055,7 @@ function classifyFilter(scan: MongoCallScan, rootIndex: number): MongoCursorClas
if (enumKey) return { mode: "enumValue", enumKey };
return { mode: scan.inString ? "none" : "value" };
}
if (inner.key === "$elemMatch") return { mode: "elemMatchKey" };
if (innerDepth(scan, rootIndex) === 0) return { mode: "filterField" };
// Inside a nested object: whose value is it?
@@ -1059,8 +1066,6 @@ function classifyFilter(scan: MongoCallScan, rootIndex: number): MongoCursorClas
const parent = scan.stack[scan.stack.length - 2];
return { mode: parent?.kind === "array" && VALUE_ARRAY_OPERATORS.has(parent.key ?? "") ? "valueWrapper" : "filterField" };
}
case "$elemMatch":
return { mode: "filterField" };
case "$expr":
return { mode: "expression" };
default:
@@ -84,6 +84,15 @@ export const TOP_LEVEL_QUERY_OPERATORS: MongoOperatorSpec[] = specs([
["$jsonSchema", "Matches documents against a JSON schema", "$jsonSchema: { ${} }"],
]);
/** Logical operators accepted inside `$elemMatch` and at the root of a filter. */
export const LOGICAL_QUERY_OPERATORS: MongoOperatorSpec[] = TOP_LEVEL_QUERY_OPERATORS.filter((op) => ["$and", "$or", "$nor"].includes(op.label));
/**
* Operators accepted directly inside the body of `$elemMatch`.
* Matches both scalar elements (via field query operators) and document elements (via field names + logical operators).
*/
export const ELEM_MATCH_QUERY_OPERATORS: MongoOperatorSpec[] = [...FIELD_QUERY_OPERATORS, ...LOGICAL_QUERY_OPERATORS];
/** Every query operator, for callers that do not care about position. */
export const QUERY_OPERATORS: MongoOperatorSpec[] = [...FIELD_QUERY_OPERATORS, ...TOP_LEVEL_QUERY_OPERATORS];