Skip to content

[Types] sort by $meta textScore is not allowed #11867

@Jule-

Description

@Jule-

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

6.3.5

Node.js version

16.14.2

MongoDB server version

4.2.3

Description

It is not possible to handle a text index search with sorting on the meta score like described in the MongoDB documentation: https://www.mongodb.com/docs/manual/core/text-search-operators/#query-framework

db.stores.find(
   { $text: { $search: "coffee shop cake" } },
   { score: { $meta: "textScore" } }
).sort( { score: { $meta: "textScore" } } )

Typescript is complaining:

Type '{ score: { $meta: string; }; }' is not assignable to type 'string | { [key: string]: SortOrder; }'.
  Types of property 'score' are incompatible.
    Type '{ $meta: string; }' is not assignable to type 'SortOrder | undefined'.ts(2345)

Steps to Reproduce

Mongoose typings should allow doing:

const stores = await Store.find(
   { $text: { $search: "coffee shop cake" } },
   { score: { $meta: "textScore" } }
).sort( { score: { $meta: "textScore" } } )

Expected Behavior

No Typescript error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugWe've confirmed this is a bug in Mongoose and will fix it.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions