MongoError: must have $meta projection for all $meta sort keys using Mongo DB Native NodeJS Driver

后端 未结 2 1552
南旧
南旧 2021-01-18 02:36

Running the following text search directly on MongoDB results in no issues:

db.getCollection(\'schools\').find({
  $text:
    {
      $search: \'some query s         


        
2条回答
  •  死守一世寂寞
    2021-01-18 03:04

    In the current version of the native MongoDB driver, you need to include the projection key among the options for find:

    const results = await collection.find(
      {
        $text: { $search: filter }
      },
      {
        projection: { score: { $meta: 'textScore' } },
        sort: { score: { $meta: 'textScore' } },
      }
    ).toArray();
    

提交回复
热议问题