I am trying to sort by $meta score the results from my $text search but every time I do I get this error message.
MongoError: must have $meta projection for all $meta sort keys
operationTime: Timestamp { _bsontype: 'Timestamp', low_: 2, high_: 1584383573 },
ok: 0,
errmsg: 'must have $meta projection for all $meta sort keys',
code: 2,
codeName: 'BadValue',
'$clusterTime':
{ clusterTime: Timestamp { _bsontype: 'Timestamp', low_: 2, high_: 1584383573 },
signature: { hash: [Object], keyId: [Object] } },
name: 'MongoError',
[Symbol(mongoErrorContextSymbol)]: {} }
What I am trying to achieve is to allow users to search words like “ah or jo” and for my search query to find words that contain the strings passed and sort them based on relevance.
I got it to work but it wasn’t finding parts of strings BUT only whole strings… For example, If I pass “Jacob” it will find names with that string but if I pass “Jac” it will come back as null.
Here is my code
var userData = await UserDB.userModel.find(
{
$or: [
{
$text: {$search: 'jac'}
},
{score : { $meta: "textScore" } } ,
{
$or: [
{'username': {$regex: '^jac'}},
{'name.first': {$regex: '^jac'}},
{'name.last': {$regex: '^jac'}}
]
}
]
}).sort( { score: { $meta: "textScore" } } )