MongoError: must have $meta projection for all $meta sort keys using Mongo DB Native NodeJS Driver
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"}})
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();