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(mongodb): read zone-less filter dates as local time
Loc Nguyen71068cef1678
diff --git a/apps/desktop/src/lib/app/documentStoreProvider.ts b/apps/desktop/src/lib/app/documentStoreProvider.tsindex bd1b942536..6cc783da24 100644--- a/apps/desktop/src/lib/app/documentStoreProvider.ts+++ b/apps/desktop/src/lib/app/documentStoreProvider.ts@@ -2,7 +2,7 @@ import type { ComposerTranslation } from "vue-i18n";import { normalizeJsonArgument } from "@dbx-app/mongo-shell";import { isElasticsearchCompatibleDatabaseType, isMeilisearchDatabaseType, isSolrDatabaseType, type DatabaseType } from "@/types/database";import { quoteUnquotedObjectKeys } from "@/lib/mongo/mongoShellCommand";-import { formatMongoShellLiteral } from "@/lib/mongo/mongoDocumentValues";+import { formatMongoShellLiteral, normalizeMongoDateInput } from "@/lib/mongo/mongoDocumentValues";export type DocumentStoreKind = "mongodb" | "dynamodb" | "elasticsearch" | "meilisearch" | "solr";export type DocumentFilterMode = "equals" | "not-equals" | "like" | "not-like" | "begins-with" | "ends-with" | "greater-than" | "greater-than-or-equal" | "less-than" | "less-than-or-equal" | "in" | "not-in" | "between" | "not-between" | "is-null" | "is-not-null";@@ -910,10 +910,12 @@ function parseMongoFilterValueAs(raw: string, valueType: Exclude<DocumentFilterVcase "object-id":if (!/^[0-9a-f]{24}$/i.test(text)) throw invalidMongoFilterValue(valueType, raw);return { $oid: text };- case "date":+ case "date": {if (/^-?\d+$/.test(text)) return { $date: { $numberLong: text } };- if (Number.isNaN(Date.parse(text))) throw invalidMongoFilterValue(valueType, raw);- return { $date: text };+ const normalized = normalizeMongoDateInput(text);+ if (!normalized) throw invalidMongoFilterValue(valueType, raw);+ return { $date: normalized };+ }case "int32": {if (!/^-?\d+$/.test(text)) throw invalidMongoFilterValue(valueType, raw);const value = BigInt(text);diff --git a/apps/desktop/src/lib/mongo/mongoDocumentValues.ts b/apps/desktop/src/lib/mongo/mongoDocumentValues.tsindex d157ff0bea..28cee05abd 100644--- a/apps/desktop/src/lib/mongo/mongoDocumentValues.ts+++ b/apps/desktop/src/lib/mongo/mongoDocumentValues.ts@@ -103,7 +103,41 @@ export function mongoShellDateToExtendedJson(value: unknown): unknown {if (typeof value !== "string") return value;const match = value.trim().match(MONGO_SHELL_DATE_PATTERN);if (!match) return value;- return { $date: match[2] };+ return { $date: normalizeMongoDateInput(match[2] ?? "") ?? match[2] };+}++/** `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. */+const MONGO_RFC3339_DATE_PATTERN = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})$/;++/**+ * Turns the date text a person types into the RFC 3339 string `$date` requires.+ *+ * The server accepts nothing else, but people paste what they see: a grid cell+ * shows `2025-04-01 19:46:03`, a log line `2025-04-01`. Those name no zone, so+ * they are read as local time, the way the person reads them, and sent as UTC.+ * Text that already spells RFC 3339 is kept as written; any other spelling the+ * platform can parse is canonicalised. Returns null when the text is no date.+ */+export function normalizeMongoDateInput(text: string): string | null {+ const trimmed = text.trim();+ if (!trimmed) return null;+ if (MONGO_RFC3339_DATE_PATTERN.test(trimmed) && !Number.isNaN(Date.parse(trimmed))) return trimmed;++ const local = MONGO_LOCAL_DATE_PATTERN.exec(trimmed);+ if (local) {+ const [, year = "", month = "", day = "", hour = "0", minute = "0", second = "0", fraction = "0"] = local;+ const parts = [Number(year), Number(month), Number(day), Number(hour), Number(minute), Number(second), Number(fraction.padEnd(3, "0"))] as const;+ const date = new Date(parts[0], parts[1] - 1, parts[2], parts[3], parts[4], parts[5], parts[6]);+ // `new Date(2025, 1, 30)` rolls over into March; a day that does not exist is a typo, not a date.+ const exists = date.getFullYear() === parts[0] && date.getMonth() === parts[1] - 1 && date.getDate() === parts[2];+ if (!exists || parts[3] > 23 || parts[4] > 59 || parts[5] > 59) return null;+ return date.toISOString();+ }++ const parsed = Date.parse(trimmed);+ return Number.isNaN(parsed) ? null : new Date(parsed).toISOString();}export function parseMongoDocumentInputValue(raw: MongoInputValue): unknown {diff --git a/docs/content/docs/mongodb.cn.mdx b/docs/content/docs/mongodb.cn.mdxindex 52aa4e8e9c..13af042e1e 100644--- a/docs/content/docs/mongodb.cn.mdx+++ b/docs/content/docs/mongodb.cn.mdx@@ -38,7 +38,7 @@ MongoDB 的总数可能使用估算值。数据持续写入时,翻页过程中## 过滤与命令-可以在文档浏览器中构建字段过滤,也可以在 MongoDB 查询标签中输入 shell 风格命令。编辑器会根据数据库、Collection、字段、查询运算符、更新运算符、聚合阶段和游标方法提供补全。+可以在文档浏览器中构建字段过滤,也可以在 MongoDB 查询标签中输入 shell 风格命令。日期类型的过滤值除 RFC 3339 外也接受 `2025-04-01 19:46:03` 或 `2025-04-01`,不带时区的文本按本地时区解析。编辑器会根据数据库、Collection、字段、查询运算符、更新运算符、聚合阶段和游标方法提供补全。常见命令包括:diff --git a/docs/content/docs/mongodb.mdx b/docs/content/docs/mongodb.mdxindex 1227ec3a5a..496e0830c1 100644--- a/docs/content/docs/mongodb.mdx+++ b/docs/content/docs/mongodb.mdx@@ -38,7 +38,7 @@ MongoDB totals may be estimated. In actively changing collections, the count and## Filters and Commands-Build field filters in the document browser or use shell-style commands in a MongoDB query tab. Completion can suggest databases, collections, fields, query operators, update operators, aggregation stages, and cursor methods.+Build field filters in the document browser or use shell-style commands in a MongoDB query tab. A date filter value accepts `2025-04-01 19:46:03` or `2025-04-01` as well as RFC 3339; text without a time zone is read in your local zone. Completion can suggest databases, collections, fields, query operators, update operators, aggregation stages, and cursor methods.Examples:diff --git a/packages/app-tests/documentStoreProvider.test.ts b/packages/app-tests/documentStoreProvider.test.tsindex c96c284266..c99fa3f0ad 100644--- a/packages/app-tests/documentStoreProvider.test.ts+++ b/packages/app-tests/documentStoreProvider.test.ts@@ -55,6 +55,31 @@ test("providers build store-specific query previews", () => {);});+test("reads zone-less date filter values as local time and sends them as UTC", () => {+ const local = (year: number, month: number, day: number, hour = 0, minute = 0, second = 0, ms = 0) => new Date(year, month - 1, day, hour, minute, second, ms).toISOString();+ const dated = { kind: "mongodb" as const, sampleValue: { $date: "2025-01-01T00:00:00Z" } };+ const condition = (rawValue: string, options: Parameters<typeof buildDocumentFilterCondition>[1] = dated) => buildDocumentFilterCondition(rule({ fieldName: "createdAt", rawValue }), options);++ // What a grid cell or a log line shows, read in the person's own zone.+ assert.deepEqual(condition("2025-04-01 19:46:03"), { createdAt: { $date: local(2025, 4, 1, 19, 46, 3) } });+ assert.deepEqual(condition("2025-04-01T19:46:03"), { createdAt: { $date: local(2025, 4, 1, 19, 46, 3) } });+ assert.deepEqual(condition("2025-04-01 19:46:03.718"), { createdAt: { $date: local(2025, 4, 1, 19, 46, 3, 718) } });+ assert.deepEqual(condition("2025-04-01 19:46"), { createdAt: { $date: local(2025, 4, 1, 19, 46) } });+ assert.deepEqual(condition("2025/4/1"), { createdAt: { $date: local(2025, 4, 1) } });+ assert.deepEqual(condition("2025-04-01"), { createdAt: { $date: local(2025, 4, 1) } });+ // Text that names its zone is kept as written when the server accepts that spelling, and canonicalised otherwise.+ assert.deepEqual(condition("2025-04-01T19:46:03+08:00"), { createdAt: { $date: "2025-04-01T19:46:03+08:00" } });+ assert.deepEqual(condition("2025-04-01T19:46:03.5Z"), { createdAt: { $date: "2025-04-01T19:46:03.5Z" } });+ assert.deepEqual(condition("2025-04-01T19:46:03+0800"), { createdAt: { $date: "2025-04-01T11:46:03.000Z" } });+ // Epoch milliseconds and an explicit value type still work.+ assert.deepEqual(condition("1752364800000"), { createdAt: { $date: { $numberLong: "1752364800000" } } });+ assert.deepEqual(buildDocumentFilterCondition(rule({ fieldName: "createdAt", rawValue: "2025-04-01 19:46:03", valueType: "date" }), { kind: "mongodb" }), { createdAt: { $date: local(2025, 4, 1, 19, 46, 3) } });+ // A day that does not exist, or text that is no date, is rejected rather than sent as a string.+ assert.throws(() => condition("2025-02-30"), /Invalid MongoDB date filter value/);+ assert.throws(() => condition("2025-04-01 25:00:00"), /Invalid MongoDB date filter value/);+ assert.throws(() => condition("yesterday"), /Invalid MongoDB date filter value/);+});+test("builds reusable document filter conditions", () => {assert.deepEqual(buildDocumentFilterCondition(rule({})), { city: "长治" });assert.deepEqual(buildDocumentFilterCondition(rule({ mode: "like", rawValue: "a.b[0]*" })), {diff --git a/packages/app-tests/mongoDocumentValues.test.ts b/packages/app-tests/mongoDocumentValues.test.tsindex c2caa6b435..7e7b609d45 100644--- a/packages/app-tests/mongoDocumentValues.test.ts+++ b/packages/app-tests/mongoDocumentValues.test.ts@@ -62,6 +62,10 @@ test("parses Mongo shell ISODate literals as extended JSON dates", () => {});test("preserves date-shaped Mongo strings instead of guessing Date", () => {+ // A shell date literal written the way a grid cell shows it is read as local time and sent as UTC.+ assert.deepEqual(parseMongoDocumentInputValue('ISODate("2025-04-01 19:46:03")'), { $date: new Date(2025, 3, 1, 19, 46, 3).toISOString() });+ assert.deepEqual(parseMongoDocumentInputValue('new Date("2025-04-01")'), { $date: new Date(2025, 3, 1).toISOString() });+ // Bare text stays text: only the literal marks a date.assert.equal(parseMongoDocumentInputValue("2025-08-14 02:25:43.718"), "2025-08-14 02:25:43.718");assert.equal(parseMongoDocumentInputValue("2025-04-01 19:46:03"), "2025-04-01 19:46:03");assert.equal(parseMongoDocumentInputValue('"2025-08-14 02:25:43.718"'), "2025-08-14 02:25:43.718");