Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

fix(mongo): stop suggesting extended json keys as fields

Loc Nguyen7c2db2d1422e
diff --git a/apps/desktop/src/lib/mongo/mongoCompletion.ts b/apps/desktop/src/lib/mongo/mongoCompletion.ts
index e41c7f2595..cd3ee1c8e4 100644
--- a/apps/desktop/src/lib/mongo/mongoCompletion.ts
+++ b/apps/desktop/src/lib/mongo/mongoCompletion.ts
@@ -1,3 +1,4 @@
+import { mongoExtendedJsonValueType } from "@/lib/mongo/mongoDocumentValues";
import { ACCUMULATORS, COMMON_OPERATORS, EXPRESSION_OPERATORS, PIPELINE_STAGES, PUSH_MODIFIERS, QUERY_OPERATORS, STAGE_OPTION_KEYS, UPDATE_OPERATORS, UPDATE_OPERATOR_LABELS, VALUE_SNIPPETS, mongoOperatorItemType, type MongoOperatorSpec } from "@/lib/mongo/mongoCompletionTables";
/**
@@ -1015,6 +1016,9 @@ function extractActiveCollection(text: string, cursor: number): string | undefin
function collectFieldTypes(value: unknown, prefix: string, out: Map<string, Set<string>>, depth: number) {
if (depth > 4 || value == null || typeof value !== "object") return;
+ // A wrapper such as {$oid: "..."} is one BSON value. Walking into it would offer
+ // `_id.$oid`, a path that exists only in transport and matches nothing on the server.
+ if (mongoExtendedJsonValueType(value)) return;
if (Array.isArray(value)) {
for (const item of value.slice(0, 3)) collectFieldTypes(item, prefix, out, depth + 1);
return;
@@ -1031,7 +1035,7 @@ function describeMongoValueType(value: unknown): string {
if (value == null) return "null";
if (Array.isArray(value)) return "array";
if (value instanceof Date) return "date";
- return typeof value === "object" ? "object" : typeof value;
+ return mongoExtendedJsonValueType(value) ?? (typeof value === "object" ? "object" : typeof value);
}
function quoteMongoFieldName(field: string, prefix: string): string {
diff --git a/apps/desktop/src/lib/mongo/mongoDocumentValues.ts b/apps/desktop/src/lib/mongo/mongoDocumentValues.ts
index 540624c5a3..03dc6bef5c 100644
--- a/apps/desktop/src/lib/mongo/mongoDocumentValues.ts
+++ b/apps/desktop/src/lib/mongo/mongoDocumentValues.ts
@@ -15,7 +15,38 @@ export const MONGO_DOCUMENT_GRID_NULL = `${MONGO_DOCUMENT_GRID_PREFIX}null`;
const MAX_SAFE_BIGINT = BigInt(Number.MAX_SAFE_INTEGER);
const MIN_BSON_INT64 = -9223372036854775808n;
const MAX_BSON_INT64 = 9223372036854775807n;
-const MONGO_EXTENDED_JSON_VALUE_KEYS = new Set(["$binary", "$code", "$date", "$dbPointer", "$maxKey", "$minKey", "$numberDecimal", "$numberDouble", "$numberInt", "$numberLong", "$oid", "$regularExpression", "$symbol", "$timestamp", "$undefined", "$uuid"]);
+/** Extended JSON wrapper key -> the BSON scalar it stands for. */
+const MONGO_EXTENDED_JSON_VALUE_TYPES = new Map([
+ ["$binary", "binary"],
+ ["$code", "javascript"],
+ ["$date", "date"],
+ ["$dbPointer", "dbPointer"],
+ ["$maxKey", "maxKey"],
+ ["$minKey", "minKey"],
+ ["$numberDecimal", "decimal128"],
+ ["$numberDouble", "double"],
+ ["$numberInt", "int32"],
+ ["$numberLong", "int64"],
+ ["$oid", "objectId"],
+ ["$regularExpression", "regex"],
+ ["$symbol", "symbol"],
+ ["$timestamp", "timestamp"],
+ ["$undefined", "undefined"],
+ ["$uuid", "uuid"],
+]);
+const MONGO_EXTENDED_JSON_VALUE_KEYS = new Set(MONGO_EXTENDED_JSON_VALUE_TYPES.keys());
+
+/**
+ * The BSON scalar an extended JSON wrapper stands for, or undefined when the value is
+ * a plain object. `{$oid: "..."}` is how the driver ships an ObjectId over JSON; it is
+ * one value, not a subdocument with a `$oid` field.
+ */
+export function mongoExtendedJsonValueType(value: unknown): string | undefined {
+ if (!value || typeof value !== "object" || Array.isArray(value)) return undefined;
+ const keys = Object.keys(value as Record<string, unknown>);
+ if (keys.length === 2 && keys.includes("$code") && keys.includes("$scope")) return "javascript";
+ return keys.length === 1 ? MONGO_EXTENDED_JSON_VALUE_TYPES.get(keys[0] ?? "") : undefined;
+}
const MONGO_EXTENDED_JSON_NUMERIC_TYPES = new Map([
["$numberInt", "int32"],
["$numberLong", "int64"],
diff --git a/packages/app-tests/mongoCompletion.test.ts b/packages/app-tests/mongoCompletion.test.ts
index c52367380f..c89e63fccc 100644
--- a/packages/app-tests/mongoCompletion.test.ts
+++ b/packages/app-tests/mongoCompletion.test.ts
@@ -466,6 +466,35 @@ test("snippet templates use placeholder syntax CodeMirror actually honours", ()
}
});
+test("treats extended JSON wrappers as scalars, not subdocuments", () => {
+ // The driver ships BSON scalars as extended JSON. Walking into them would offer
+ // `_id.$oid`, which is valid syntax that matches nothing on the server.
+ const inferred = inferMongoCompletionFields([
+ {
+ _id: { $oid: "6743e4bfa3f6f84bc3fff6c8" },
+ created_at: { $date: "2025-01-01T00:00:00Z" },
+ count: { $numberLong: "42" },
+ raw: { $binary: { base64: "AQID", subType: "00" } },
+ profile: { email: "[email protected]" },
+ },
+ ]);
+
+ for (const phantom of ["_id.$oid", "created_at.$date", "count.$numberLong", "raw.$binary"]) {
+ assert.equal(
+ inferred.some((field) => field.name === phantom),
+ false,
+ phantom,
+ );
+ }
+
+ assert.ok(inferred.find((field) => field.name === "_id" && field.type === "objectId"));
+ assert.ok(inferred.find((field) => field.name === "created_at" && field.type === "date"));
+ assert.ok(inferred.find((field) => field.name === "count" && field.type === "int64"));
+ assert.ok(inferred.find((field) => field.name === "raw" && field.type === "binary"));
+ // Genuine subdocuments are still walked.
+ assert.ok(inferred.find((field) => field.name === "profile.email" && field.type === "string"));
+});
+
test("infers dotted MongoDB fields from sampled documents", () => {
const inferred = inferMongoCompletionFields([
{ _id: "1", profile: { email: "[email protected]" }, tags: ["a"] },