Skip to content

Commit 1a21b3f

Browse files
committed
feat(crud): do not apply default sort option for views and timeseries COMPASS-9749
1 parent f1c5f4f commit 1a21b3f

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

packages/compass-crud/src/stores/crud-store.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ export type CrudStoreOptions = Pick<
269269
| 'namespace'
270270
| 'isTimeSeries'
271271
| 'isSearchIndexesSupported'
272+
| 'sourceName'
272273
> & {
273274
noRefreshOnConfigure?: boolean;
274275
};
@@ -1646,12 +1647,19 @@ class CrudStoreImpl
16461647
countOptions.hint = '_id_';
16471648
}
16481649

1650+
const isView = this.options.isReadonly && this.options.sourceName;
1651+
// Default sort options that we allow to choose from in settings will have a
1652+
// massive negative effect on the query performance for views and view-like
1653+
// collections in all cases. To avoid that, we're not applying default sort
1654+
// for those
1655+
const allowDefaultSort = !isView && !this.options.isTimeSeries;
1656+
1657+
const { defaultSortOrder } = this.preferences.getPreferences();
1658+
16491659
let sort = query.sort;
1650-
if (!sort && this.preferences.getPreferences().defaultSortOrder) {
1651-
sort = validate(
1652-
'sort',
1653-
this.preferences.getPreferences().defaultSortOrder
1654-
);
1660+
1661+
if (!sort && allowDefaultSort && defaultSortOrder) {
1662+
sort = validate('sort', defaultSortOrder);
16551663
}
16561664

16571665
const findOptions = {

packages/compass-preferences-model/src/preferences-schema.tsx

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@ const enableDbAndCollStatsDescription: React.ReactNode = (
3737

3838
export const SORT_ORDER_VALUES = [
3939
'',
40-
'{ $natural: -1 }',
4140
'{ _id: 1 }',
4241
'{ _id: -1 }',
42+
'{ $natural: -1 }',
4343
] as const;
44+
4445
export type SORT_ORDERS = typeof SORT_ORDER_VALUES[number];
4546

4647
export type PermanentFeatureFlags = {
@@ -630,23 +631,24 @@ export const storedUserPreferencesProps: Required<{
630631
global: true,
631632
description: {
632633
short: 'Default Sort for Query Bar',
633-
long: 'All queries executed from the query bar will apply this sort.',
634+
long: 'All queries executed from the query bar will apply this sort. Not available for views and timeseries.',
634635
options: {
635636
'': {
636-
label: '$natural: 1 (MongoDB server default)',
637-
description: 'in natural order of documents',
638-
},
639-
'{ $natural: -1 }': {
640-
label: '$natural: -1',
641-
description: 'in reverse natural order of documents',
637+
label: 'MongoDB server default',
638+
description: 'Return documents in natural order of documents',
642639
},
643640
'{ _id: 1 }': {
644641
label: '_id: 1',
645-
description: 'in ascending order by id',
642+
description: 'Return documents in ascending order by id',
646643
},
647644
'{ _id: -1 }': {
648645
label: '_id: -1',
649-
description: 'in descending order by id',
646+
description: 'Return documents in in descending order by id',
647+
},
648+
'{ $natural: -1 }': {
649+
label: '$natural: -1',
650+
description:
651+
'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.',
650652
},
651653
},
652654
},

0 commit comments

Comments
 (0)