MongoError:必须对所有使用Mongo DB本地NodeJS驱动的$meta排序键都有$meta投影

时间:2021-03-23 07:00:54

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

直接在MongoDB上运行以下文本搜索,没有问题:

db.getCollection('schools').find({
  $text:
    {
      $search: 'some query string',
      $caseSensitive: false,
      $diacriticSensitive: true
    }
}, {score: {$meta: "textScore"}}).sort({score:{$meta:"textScore"}})

However when trying to run the same query using the native NodeJS driver:

但是,当尝试使用本地NodeJS驱动程序运行相同的查询时:

function getSchools(filter) {
  return new Promise(function (resolve, reject) {

    MongoClient.connect('mongodb://localhost:60001', function(err, client) {
      const collection = client.db('schools').collection('schools');

      collection.find({
        $text:
          {
            $search: filter,
            $caseSensitive: false,
            $diacriticSensitive: true
          }
        }, {score: {$meta: "textScore"}}).sort({score:{$meta:"textScore"}}).toArray(function(err, docs) {
        if (err) return reject(err);

        resolve(docs);
      });
    });
  });
}

I'm getting the following error:

我得到以下错误:

MongoError: must have $meta projection for all $meta sort keys

What am I doing wrong here?

我在这里做错了什么?

1 个解决方案

#1


3  

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:

好的,根据这个bug,因为3.0.0版本的find和findOne不再支持字段参数,查询需要重写如下:

collection.find({
        $text:
          {
            $search: filter,
            $caseSensitive: false,
            $diacriticSensitive: true
          }
        })
        .project({ score: { $meta: "textScore" } })
        .sort({score:{$meta:"textScore"}})

#1


3  

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:

好的,根据这个bug,因为3.0.0版本的find和findOne不再支持字段参数,查询需要重写如下:

collection.find({
        $text:
          {
            $search: filter,
            $caseSensitive: false,
            $diacriticSensitive: true
          }
        })
        .project({ score: { $meta: "textScore" } })
        .sort({score:{$meta:"textScore"}})