Type alias StrictSort<Schema>

StrictSort<Schema>: {
    [K in keyof ToDotNotation<WithId<Schema>>]?: SortDirection
} | {
    $vector: number[];
} | {
    $vectorize: string;
}

Specifies the sort criteria for selecting documents.

Can use 1/-1 for ascending/descending, or $vector for sorting by vector distance.

See SortDirection for all possible sort values.

NB. The order of the fields in the sort option is significant—fields are sorted in the order they are listed.

Type Parameters

Type declaration

  • $vector: number[]

Type declaration

  • $vectorize: string

Example

// Sort by name in ascending order, then by age in descending order
await collection.findOne({}, {
  sort: {
  name: 1,
  age: -1,
  } satisfies StrictSort<SomeDoc>,
});

// Sort by vector distance
await collection.findOne({}, {
  sort: {
  $vector: [0.23, 0.38, 0.27, 0.91, 0.21],
  } satisfies StrictSort<SomeDoc>,
});

See

  • Sort
  • SortDirection