asl*_*ski 3 full-text-search mongodb node.js mongodb-query node-mongodb-native
直接在MongoDB上运行以下文本搜索没有问题:
db.getCollection('schools').find({
$text:
{
$search: 'some query string',
$caseSensitive: false,
$diacriticSensitive: true
}
}, {score: {$meta: "textScore"}}).sort({score:{$meta:"textScore"}})
Run Code Online (Sandbox Code Playgroud)
但是,当尝试使用本机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);
});
});
});
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
MongoError: must have $meta projection for all $meta sort keys
Run Code Online (Sandbox Code Playgroud)
我在这做错了什么?
OK,根据这个bug,因为3.0.0版本find,并findOne 不再支持的fields参数和查询需要如下重写:
collection.find({
$text:
{
$search: filter,
$caseSensitive: false,
$diacriticSensitive: true
}
})
.project({ score: { $meta: "textScore" } })
.sort({score:{$meta:"textScore"}})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1964 次 |
| 最近记录: |