直接在MongoDB上运行以下文本搜索不会产生任何问题:
db.getCollection('schools').find({
$text:
{
$search: 'some query string',
$caseSensitive: false,
$diacriticSensitive: true
}
}, {score: {$meta: "textScore"}}).sort({score:{$meta:"textScore"}})
但是,当尝试使用native NodeJS driver:
运行相同的查询时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);
});
});
});
}
我收到以下错误:
MongoError: must have $meta projection for all $meta sort keys
我在这里做错了什么?
答案 0 :(得分:7)
好的,根据this bug自版本3.0.0 npm install firebase-functions@latest firebase-admin@latest --save
npm install -g firebase-tools
和find
no longer support findOne
参数和查询需要重写如下:
fields
答案 1 :(得分:0)
在当前版本的本机MongoDB驱动程序中,您需要包括projection
key among the options for find
:
const results = await collection.find(
{
$text: { $search: filter }
},
{
projection: { score: { $meta: 'textScore' } },
sort: { score: { $meta: 'textScore' } },
}
).toArray();