From 71068cef167829a78ffee69f5f509f265f7fe180 Mon Sep 17 00:00:00 2001 From: Loc Nguyen <113083148+thailoc-dev@users.noreply.github.com> Date: Wed, 30 Sep 2026 14:00:53 +0700 Subject: [PATCH] fix(mongodb): read zone-less filter dates as local time --- .../src/lib/app/documentStoreProvider.ts | 10 +++--- .../src/lib/mongo/mongoDocumentValues.ts | 36 ++++++++++++++++++- docs/content/docs/mongodb.cn.mdx | 2 +- docs/content/docs/mongodb.mdx | 2 +- .../app-tests/documentStoreProvider.test.ts | 25 +++++++++++++ .../app-tests/mongoDocumentValues.test.ts | 4 +++ 6 files changed, 72 insertions(+), 7 deletions(-) diff --git a/apps/desktop/src/lib/app/documentStoreProvider.ts b/apps/desktop/src/lib/app/documentStoreProvider.ts index 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 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.mdx index 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.mdx index 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.ts index 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[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.ts index 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");