From e7ec2ecaf2ff89c4fa1d8d77c27577b7c2516a65 Mon Sep 17 00:00:00 2001 From: gggdsg <76191330+xiaoge19961220@users.noreply.github.com> Date: Fri, 3 Jul 2026 15:47:38 +0800 Subject: [PATCH] fix(mongodb): route find result sorting through shell sort clauses Use find().sort() for MongoDB query grid sorting instead of SQL ORDER BY, and normalize document browser sort input the same way as filters. Co-authored-by: Cursor --- .../components/document/DocumentBrowser.vue | 15 ++++++-- .../src/composables/useDataGridActions.ts | 17 +++++++++ apps/desktop/src/lib/documentStoreProvider.ts | 5 +++ apps/desktop/src/lib/mongoShellCommand.ts | 36 +++++++++++++++++++ 4 files changed, 71 insertions(+), 2 deletions(-) diff --git a/apps/desktop/src/components/document/DocumentBrowser.vue b/apps/desktop/src/components/document/DocumentBrowser.vue index bd689bb75..abf180962 100644 --- a/apps/desktop/src/components/document/DocumentBrowser.vue +++ b/apps/desktop/src/components/document/DocumentBrowser.vue @@ -16,7 +16,18 @@ import * as api from "@/lib/api"; import { useConnectionStore } from "@/stores/connectionStore"; import { clampSearchSplitWidth } from "@/lib/dataGridSearchSplit"; import { documentViewerFontStyle } from "@/lib/documentViewerFontStyle"; -import { buildDocumentFilterCondition, combineDocumentFilterConditions, currentDocumentFilterJson, defaultDocumentFilterRule, documentFilterModeNeedsValue, documentFilterModeOptions, documentStoreProviderFor, type DocumentFilterMode, type DocumentFilterRule } from "@/lib/documentStoreProvider"; +import { + buildDocumentFilterCondition, + combineDocumentFilterConditions, + currentDocumentFilterJson, + currentDocumentSortJson, + defaultDocumentFilterRule, + documentFilterModeNeedsValue, + documentFilterModeOptions, + documentStoreProviderFor, + type DocumentFilterMode, + type DocumentFilterRule, +} from "@/lib/documentStoreProvider"; import { buildMongoInsertDocument, buildMongoUpdateDocument, formatMongoShellLiteral, parseMongoDocumentInputValue, type MongoInputValue } from "@/lib/mongoDocumentValues"; import { normalizeResultPageSize } from "@/lib/paginationPageSize"; import { useSettingsStore } from "@/stores/settingsStore"; @@ -433,7 +444,7 @@ async function load() { const previousSelectedId = previousSelectedIdx === null ? null : documentIdentity(documents.value[previousSelectedIdx]); try { const filter = currentDocumentFilter(); - const sort = sortInput.value.trim() || undefined; + const sort = currentDocumentSortJson(sortInput.value); const result = await api.documentFindDocuments(props.connectionId, props.database, props.collection, page.value * pageSize.value, pageSize.value, filter, undefined, sort, executionId); if (documentLoadExecutionId.value !== executionId) return; const nextDocuments = result.documents.map(asRecord); diff --git a/apps/desktop/src/composables/useDataGridActions.ts b/apps/desktop/src/composables/useDataGridActions.ts index ece2c9301..05b41af04 100644 --- a/apps/desktop/src/composables/useDataGridActions.ts +++ b/apps/desktop/src/composables/useDataGridActions.ts @@ -10,6 +10,7 @@ import * as api from "@/lib/api"; import type { QueryTab } from "@/types/database"; import { useToast } from "@/composables/useToast"; import { effectiveDatabaseTypeForConnection, metadataSchemaForConnection } from "@/lib/jdbcDialect"; +import { applyMongoFindSort } from "@/lib/mongoShellCommand"; import { uuid } from "@/lib/utils"; import type { DataGridSortMode } from "@/lib/dataGridSort"; @@ -235,6 +236,22 @@ export function useDataGridActions(activeTab: ComputedRef) } const config = connectionStore.getConfig(tab.connectionId); + if (effectiveDatabaseTypeForConnection(config) === "mongodb") { + const sortedSql = applyMongoFindSort(baseSql, column, direction); + if (!sortedSql) { + toast(t("grid.sortUnsupported"), 5000); + return; + } + queryStore.updateSql(tab.id, sortedSql); + await queryStore.executeTabSql(tab.id, sortedSql, { + resultBaseSql: baseSql, + resultSortedSql: sortedSql, + preserveResultDuringExecution: true, + preserveTotalRowCountDuringExecution: true, + }); + return; + } + const built = await api.buildSortedQuerySql({ originalSql: baseSql, databaseType: effectiveDatabaseTypeForConnection(config), diff --git a/apps/desktop/src/lib/documentStoreProvider.ts b/apps/desktop/src/lib/documentStoreProvider.ts index 4e879f994..59e18f0a7 100644 --- a/apps/desktop/src/lib/documentStoreProvider.ts +++ b/apps/desktop/src/lib/documentStoreProvider.ts @@ -151,6 +151,11 @@ export function currentDocumentFilterJson(input: string, structured: Record