From 662d2c08fe360c8fd4001189eda2cf0e6fa1b6b1 Mon Sep 17 00:00:00 2001 From: Yarchik Date: Thu, 2 Jul 2026 10:57:56 +0100 Subject: [PATCH] fix: preserve $meta direction in [field, direction] sort pair formatSort's pairToMap wrapped the direction in an array (prepareDirection([v[1]])) unlike every sibling converter. Scalar directions survive the array-to-string coercion, but a $meta object does not (isMeta is false for an array), so the documented cursor.sort(['score', { $meta: 'textScore' }]) throws Invalid sort direction while the object and deep-array forms work. Pass the raw value through, as the other converters do. --- src/sort.ts | 2 +- test/unit/sort.test.ts | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 test/unit/sort.test.ts diff --git a/src/sort.ts b/src/sort.ts index 0b239ddd494..7625937ca56 100644 --- a/src/sort.ts +++ b/src/sort.ts @@ -81,7 +81,7 @@ function isReadonlyArray(value: any): value is readonly T[] { /** @internal */ function pairToMap(v: readonly [string, SortDirection]): SortForCmd { - return new Map([[`${v[0]}`, prepareDirection([v[1]])]]); + return new Map([[`${v[0]}`, prepareDirection(v[1])]]); } /** @internal */ diff --git a/test/unit/sort.test.ts b/test/unit/sort.test.ts new file mode 100644 index 00000000000..19669feff36 --- /dev/null +++ b/test/unit/sort.test.ts @@ -0,0 +1,21 @@ +import { expect } from 'chai'; + +import { formatSort } from '../mongodb'; + +describe('formatSort', function () { + context('when the sort is a [field, direction] pair', function () { + it('formats a numeric direction', function () { + expect(formatSort(['alpha', -1])).to.deep.equal(new Map([['alpha', -1]])); + }); + + it('formats a string direction', function () { + expect(formatSort(['alpha', 'asc'])).to.deep.equal(new Map([['alpha', 1]])); + }); + + it('preserves a $meta direction', function () { + expect(formatSort(['alpha', { $meta: 'textScore' }])).to.deep.equal( + new Map([['alpha', { $meta: 'textScore' }]]) + ); + }); + }); +});