diff --git a/packages/compass-crud/src/stores/crud-store.ts b/packages/compass-crud/src/stores/crud-store.ts index d05cc90ec27..8f489cb2dd5 100644 --- a/packages/compass-crud/src/stores/crud-store.ts +++ b/packages/compass-crud/src/stores/crud-store.ts @@ -269,6 +269,7 @@ export type CrudStoreOptions = Pick< | 'namespace' | 'isTimeSeries' | 'isSearchIndexesSupported' + | 'sourceName' > & { noRefreshOnConfigure?: boolean; }; @@ -1652,12 +1653,19 @@ class CrudStoreImpl countOptions.hint = '_id_'; } + const isView = this.options.isReadonly && this.options.sourceName; + // Default sort options that we allow to choose from in settings will have a + // massive negative effect on the query performance for views and view-like + // collections in all cases. To avoid that, we're not applying default sort + // for those + const allowDefaultSort = !isView && !this.options.isTimeSeries; + + const { defaultSortOrder } = this.preferences.getPreferences(); + let sort = query.sort; - if (!sort && this.preferences.getPreferences().defaultSortOrder) { - sort = validate( - 'sort', - this.preferences.getPreferences().defaultSortOrder - ); + + if (!sort && allowDefaultSort && defaultSortOrder) { + sort = validate('sort', defaultSortOrder); } const findOptions = { diff --git a/packages/compass-preferences-model/src/preferences-schema.tsx b/packages/compass-preferences-model/src/preferences-schema.tsx index b2e6754f0d6..0ee39436d04 100644 --- a/packages/compass-preferences-model/src/preferences-schema.tsx +++ b/packages/compass-preferences-model/src/preferences-schema.tsx @@ -37,10 +37,11 @@ const enableDbAndCollStatsDescription: React.ReactNode = ( export const SORT_ORDER_VALUES = [ '', - '{ $natural: -1 }', '{ _id: 1 }', '{ _id: -1 }', + '{ $natural: -1 }', ] as const; + export type SORT_ORDERS = (typeof SORT_ORDER_VALUES)[number]; export type PermanentFeatureFlags = { @@ -630,23 +631,30 @@ export const storedUserPreferencesProps: Required<{ global: true, description: { short: 'Default Sort for Query Bar', - long: 'All queries executed from the query bar will apply this sort.', + long: 'All queries executed from the query bar will apply this sort. Not available for views and timeseries.', + longReact: ( + <> + All queries executed from the query bar will apply this sort.{' '} + Not available for views and timeseries. + + ), options: { '': { - label: '$natural: 1 (MongoDB server default)', - description: 'in natural order of documents', - }, - '{ $natural: -1 }': { - label: '$natural: -1', - description: 'in reverse natural order of documents', + label: 'MongoDB server default', + description: 'Return documents in natural order of documents', }, '{ _id: 1 }': { label: '_id: 1', - description: 'in ascending order by id', + description: 'Return documents in ascending order by id', }, '{ _id: -1 }': { label: '_id: -1', - description: 'in descending order by id', + description: 'Return documents in in descending order by id', + }, + '{ $natural: -1 }': { + label: '$natural: -1', + description: + 'Return documents in reverse natural order, but ignores existing indexes. ⚠️ Suitable if you use Compass only with development clusters. Avoid this option if you connect to production clusters as well.', }, }, },