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

后端 未结 2 1523
南旧
南旧 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 02:57

    OK, according to this bug since the version 3.0.0 find and findOne no longer support the fields parameter and the query needs to be rewritten as follows:

    collection.find({
            $text:
              {
                $search: filter,
                $caseSensitive: false,
                $diacriticSensitive: true
              }
            })
            .project({ score: { $meta: "textScore" } })
            .sort({score:{$meta:"textScore"}})
    
    0 讨论(0)
  • 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();
    
    0 讨论(0)
提交回复
热议问题