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。
TypeScript aicliclickhousedatabasedatabase-clientdatabase-managementdockerguimcpmongodbmysqlpostgresqlredisrustsql-serversqlitetaurivue
fix(mongo): preserve custom _id on document insert and copy
lxk955e1e061419924
diff --git a/apps/desktop/src/lib/mongo/__tests__/mongoDocumentValues.spec.ts b/apps/desktop/src/lib/mongo/__tests__/mongoDocumentValues.spec.tsindex 8639aa6f75..8aa29e7062 100644--- a/apps/desktop/src/lib/mongo/__tests__/mongoDocumentValues.spec.ts+++ b/apps/desktop/src/lib/mongo/__tests__/mongoDocumentValues.spec.ts@@ -1,6 +1,17 @@import { describe, expect, it } from "vitest";import type { ColumnFormatterConfig } from "@/lib/dataGrid/columnFormatter";-import { mongoDocumentGridClipboardText, mongoDocumentGridColumnTypes, mongoDocumentGridDisplayText, mongoDocumentGridEditorText, mongoDocumentGridExternalValue, mongoDocumentGridValue } from "@/lib/mongo/mongoDocumentValues";+import {+ buildMongoCopyDocumentFromOriginal,+ buildMongoInsertDocument,+ mongoDocumentGridClipboardText,+ mongoDocumentGridColumnTypes,+ mongoDocumentGridDisplayText,+ mongoDocumentGridEditorText,+ mongoDocumentGridExternalValue,+ mongoDocumentGridValue,+ mongoShellObjectIdToExtendedJson,+ parseMongoInsertIdValue,+} from "@/lib/mongo/mongoDocumentValues";function dateFormatter(timezone: string | undefined): ColumnFormatterConfig {return { kind: "datetime", unit: "auto", pattern: "YYYY-MM-DD HH:mm:ss", timezone };@@ -87,3 +98,55 @@ describe("MongoDB array structure display", () => {expect(mongoDocumentGridDisplayText(value)).toBe('{"d": {"$date": NumberLong("9223372036854775807")}}');});});++describe("MongoDB custom _id column support (#11523)", () => {+ it("parses shell ObjectId wrappers into extended JSON objects", () => {+ expect(mongoShellObjectIdToExtendedJson('ObjectId("507f1f77bcf86cd799439011")')).toEqual({ $oid: "507f1f77bcf86cd799439011" });+ expect(mongoShellObjectIdToExtendedJson("ObjectId('507f1f77bcf86cd799439011')")).toEqual({ $oid: "507f1f77bcf86cd799439011" });+ expect(mongoShellObjectIdToExtendedJson("ObjectId(507f1f77bcf86cd799439011)")).toEqual({ $oid: "507f1f77bcf86cd799439011" });+ expect(mongoShellObjectIdToExtendedJson('new ObjectId("507f1f77bcf86cd799439011")')).toEqual({ $oid: "507f1f77bcf86cd799439011" });+ expect(mongoShellObjectIdToExtendedJson("plain-string")).toBe("plain-string");+ });++ it("parses insert id values correctly", () => {+ expect(parseMongoInsertIdValue(null)).toBeUndefined();+ expect(parseMongoInsertIdValue(undefined)).toBeUndefined();+ expect(parseMongoInsertIdValue("")).toBeUndefined();+ expect(parseMongoInsertIdValue(" ")).toBeUndefined();+ expect(parseMongoInsertIdValue("user_custom_101")).toBe("user_custom_101");+ expect(parseMongoInsertIdValue(42)).toBe(42);+ expect(parseMongoInsertIdValue("42")).toBe(42);+ expect(parseMongoInsertIdValue("507f1f77bcf86cd799439011")).toEqual({ $oid: "507f1f77bcf86cd799439011" });+ expect(parseMongoInsertIdValue('ObjectId("507f1f77bcf86cd799439011")')).toEqual({ $oid: "507f1f77bcf86cd799439011" });+ });++ it("preserves custom _id when building insert document", () => {+ expect(buildMongoInsertDocument(["custom-id", "test"], ["_id", "title"])).toEqual({+ _id: "custom-id",+ title: "test",+ });+ expect(buildMongoInsertDocument([1001, "test"], ["_id", "title"])).toEqual({+ _id: 1001,+ title: "test",+ });+ expect(buildMongoInsertDocument([null, "test"], ["_id", "title"])).toEqual({+ title: "test",+ });+ expect(buildMongoInsertDocument(["", "test"], ["_id", "title"])).toEqual({+ title: "test",+ });+ });++ it("preserves edited _id when copying original document", () => {+ const original = { _id: { $oid: "507f1f77bcf86cd799439011" }, name: "original" };+ // _id was not edited: should be excluded when excludePrimaryKeys is true+ expect(buildMongoCopyDocumentFromOriginal(original, ["507f1f77bcf86cd799439011", "modified"], ["_id", "name"], [false, true], { excludePrimaryKeys: true })).toEqual({+ name: "modified",+ });+ // _id was edited: should be preserved even with excludePrimaryKeys+ expect(buildMongoCopyDocumentFromOriginal(original, ["custom-clone-id", "modified"], ["_id", "name"], [true, true], { excludePrimaryKeys: true })).toEqual({+ _id: "custom-clone-id",+ name: "modified",+ });+ });+});diff --git a/apps/desktop/src/lib/mongo/mongoDocumentValues.ts b/apps/desktop/src/lib/mongo/mongoDocumentValues.tsindex b738186697..1a27c242fc 100644--- a/apps/desktop/src/lib/mongo/mongoDocumentValues.ts+++ b/apps/desktop/src/lib/mongo/mongoDocumentValues.ts@@ -5,6 +5,7 @@ export type MongoInputValue = string | number | boolean | null;const MONGO_SHELL_DATE_PATTERN = /^(?:ISODate|new Date)\(\s*(["'])(.+)\1\s*\)$/;const MONGO_SHELL_NUMBER_LONG_PATTERN = /^NumberLong\(\s*(["'])(-?\d+)\1\s*\)$/;+const MONGO_SHELL_OBJECT_ID_PATTERN = /^(?:ObjectId|new ObjectId)\(\s*(?:(["'])([a-fA-F0-9]{24})\1|([a-fA-F0-9]{24}))\s*\)$/;const MONGO_OBJECT_ID_PATTERN = /^[a-fA-F0-9]{24}$/;const MONGO_INTEGER_PATTERN = /^-?\d+$/;// These values are internal to the MongoDB collection grid. BSON strings may@@ -106,6 +107,13 @@ export function mongoShellDateToExtendedJson(value: unknown): unknown {return { $date: normalizeMongoDateInput(match[2] ?? "") ?? match[2] };}+export function mongoShellObjectIdToExtendedJson(value: unknown): unknown {+ if (typeof value !== "string") return value;+ const match = value.trim().match(MONGO_SHELL_OBJECT_ID_PATTERN);+ if (!match) return value;+ return { $oid: match[2] ?? match[3] };+}+/** `2025-04-01 19:46:03`, `2025/04/01`, `2025-04-01T19:46` … : a date with no zone, as people read one off a screen. */const MONGO_LOCAL_DATE_PATTERN = /^(\d{4})[-/](\d{1,2})[-/](\d{1,2})(?:[T ](\d{1,2}):(\d{2})(?::(\d{2})(?:\.(\d{1,3})\d*)?)?)?$/;/** The one spelling the server's `$date` parser accepts, kept as written. */@@ -142,6 +150,7 @@ export function normalizeMongoDateInput(text: string): string | null {export function parseMongoDocumentInputValue(raw: MongoInputValue): unknown {if (raw === null || typeof raw === "number" || typeof raw === "boolean") return raw;+ if (typeof raw !== "string") return raw;const trimmed = raw.trim();if (trimmed === "NULL") return null;@@ -153,6 +162,8 @@ export function parseMongoDocumentInputValue(raw: MongoInputValue): unknown {if (shellDate !== trimmed) return shellDate;const shellNumberLong = mongoShellNumberLongToExtendedJson(trimmed);if (shellNumberLong !== trimmed) return shellNumberLong;+ const shellObjectId = mongoShellObjectIdToExtendedJson(trimmed);+ if (shellObjectId !== trimmed) return shellObjectId;if (MONGO_INTEGER_PATTERN.test(trimmed)) {const integer = BigInt(trimmed);@@ -560,12 +571,29 @@ export function applyMongoGridChangesToDocumentBaseline(baselineDocuments: unkno});}+export function parseMongoInsertIdValue(val: MongoInputValue | unknown): unknown {+ if (val === null || val === undefined) return undefined;+ if (typeof val === "string") {+ const trimmed = val.trim();+ if (trimmed === "") return undefined;+ const shellObjectId = mongoShellObjectIdToExtendedJson(trimmed);+ if (shellObjectId !== trimmed) return shellObjectId;+ if (MONGO_OBJECT_ID_PATTERN.test(trimmed)) return { $oid: trimmed };+ }+ return val === MONGO_DOCUMENT_GRID_NULL ? null : (mongoDocumentGridEscapedString(val) ?? parseMongoDocumentInputValue(val as MongoInputValue));+}+export function buildMongoInsertDocument(row: MongoInputValue[], columns: string[]): Record<string, unknown> {const doc: Record<string, unknown> = {};for (let ci = 0; ci < columns.length; ci++) {const col = columns[ci];- if (!col || col === "_id") continue;+ if (!col) continue;const val = row[ci];+ if (col === "_id") {+ const parsedId = parseMongoInsertIdValue(val);+ if (parsedId !== undefined) doc[col] = parsedId;+ continue;+ }if (val === null) continue;doc[col] = val === MONGO_DOCUMENT_GRID_NULL ? null : (mongoDocumentGridEscapedString(val) ?? parseMongoDocumentInputValue(val));}@@ -578,11 +606,12 @@ export function buildMongoCopyInsertDocument(row: MongoInputValue[], columns: stconst col = columns[ci];if (!col || (options.excludePrimaryKeys && col === "_id")) continue;const val = row[ci];- if (val === null) continue;- if (col === "_id" && typeof val === "string" && MONGO_OBJECT_ID_PATTERN.test(val)) {- doc[col] = { $oid: val };+ if (col === "_id") {+ const parsedId = parseMongoInsertIdValue(val);+ if (parsedId !== undefined) doc[col] = parsedId;continue;}+ if (val === null) continue;doc[col] = val === MONGO_DOCUMENT_GRID_NULL ? null : (mongoDocumentGridEscapedString(val) ?? parseMongoDocumentInputValue(val));}return doc;@@ -595,16 +624,23 @@ export function buildMongoCopyDocumentFromOriginal(original: unknown, row: Mongoconst document: Record<string, unknown> = {};for (let columnIndex = 0; columnIndex < columns.length; columnIndex++) {const column = columns[columnIndex];- if (!column || (options.excludePrimaryKeys && column === "_id")) continue;+ if (!column) continue;// Display strings are ambiguous, so only explicitly edited cells may replace original BSON values.if (dirtyColumns[columnIndex]) {const value = row[columnIndex];+ if (column === "_id") {+ const parsedId = parseMongoInsertIdValue(value);+ if (parsedId !== undefined) document[column] = parsedId;+ continue;+ }if (value !== null) {document[column] = value === MONGO_DOCUMENT_GRID_NULL ? null : (mongoDocumentGridEscapedString(value) ?? parseMongoDocumentInputValue(value));}continue;}++ if (options.excludePrimaryKeys && column === "_id") continue;if (Object.prototype.hasOwnProperty.call(source, column)) document[column] = source[column];}return document;diff --git a/packages/app-tests/mongoDocumentValues.test.ts b/packages/app-tests/mongoDocumentValues.test.tsindex 2edcf8a7da..0819307e28 100644--- a/packages/app-tests/mongoDocumentValues.test.ts+++ b/packages/app-tests/mongoDocumentValues.test.ts@@ -241,10 +241,32 @@ test("applies sorted Mongo grid edits to a cloned BSON baseline by document id",]);});-test("builds Mongo inserts with parsed date values", () => {- assert.deepEqual(buildMongoInsertDocument(["ignored", 'new Date("2026-06-10T13:59:31.287Z")'], ["_id", "createdAt"]), {+test("builds Mongo inserts with parsed date values and omits empty _id", () => {+ assert.deepEqual(buildMongoInsertDocument([null, 'new Date("2026-06-10T13:59:31.287Z")'], ["_id", "createdAt"]), {createdAt: { $date: "2026-06-10T13:59:31.287Z" },});+ assert.deepEqual(buildMongoInsertDocument(["", 'new Date("2026-06-10T13:59:31.287Z")'], ["_id", "createdAt"]), {+ createdAt: { $date: "2026-06-10T13:59:31.287Z" },+ });+});++test("builds Mongo inserts preserving custom _id values (#11523)", () => {+ assert.deepEqual(buildMongoInsertDocument(["custom_id_1", "Lin"], ["_id", "name"]), {+ _id: "custom_id_1",+ name: "Lin",+ });+ assert.deepEqual(buildMongoInsertDocument(["42", "Lin"], ["_id", "name"]), {+ _id: 42,+ name: "Lin",+ });+ assert.deepEqual(buildMongoInsertDocument(['ObjectId("507f1f77bcf86cd799439011")', "Lin"], ["_id", "name"]), {+ _id: { $oid: "507f1f77bcf86cd799439011" },+ name: "Lin",+ });+ assert.deepEqual(buildMongoInsertDocument(["507f1f77bcf86cd799439011", "Lin"], ["_id", "name"]), {+ _id: { $oid: "507f1f77bcf86cd799439011" },+ name: "Lin",+ });});test("builds Mongo copy inserts with ObjectId and parsed document values", () => {@@ -294,6 +316,18 @@ test("applies only explicit Mongo grid edits to copied original documents", () =});});+test("applies custom _id when explicitly edited on copied original documents (#11523)", () => {+ assert.deepEqual(buildMongoCopyDocumentFromOriginal({ _id: { $oid: "507f1f77bcf86cd799439011" }, count: "123", profile: { role: "admin" } }, ["custom_id_2", "456", '{"role":"maintainer"}'], ["_id", "count", "profile"], [true, true, false], { excludePrimaryKeys: true }), {+ _id: "custom_id_2",+ count: 456,+ profile: { role: "admin" },+ });+ assert.deepEqual(buildMongoCopyDocumentFromOriginal({ _id: { $oid: "507f1f77bcf86cd799439011" }, count: "123", profile: { role: "admin" } }, ["", "456", '{"role":"maintainer"}'], ["_id", "count", "profile"], [true, true, false], { excludePrimaryKeys: true }), {+ count: 456,+ profile: { role: "admin" },+ });+});+test("formats extended JSON dates as Mongo shell ISODate literals", () => {assert.equal(formatMongoShellLiteral({@@ -405,7 +439,11 @@ test("keeps explicit Mongo null fields distinct from missing fields during grid_id: "1",nullable: null,});+ assert.deepEqual(buildMongoInsertDocument([null, MONGO_DOCUMENT_GRID_NULL, null], columns), {+ nullable: null,+ });assert.deepEqual(buildMongoInsertDocument(["1", MONGO_DOCUMENT_GRID_NULL, null], columns), {+ _id: 1,nullable: null,});assert.deepEqual(buildMongoCopyInsertDocument(["1", MONGO_DOCUMENT_GRID_NULL, null], columns), {