I have the following find query that retrieves results ordered by textScore, but how can I apply criteria to only return documents that have text score of greater than 1?
db.foods.find(
{ $text: { $search: "red blue green" } },
{ score: { $meta: "textScore" } }
).sort( { score: { $meta: "textScore" } } )
Use the aggregation framework to get the documents that have a text score of greater than 1 by doing a $match
pipeline operation which will match on either the term "red" or "blue" or "green", followed by a $project
operator pipeline which projects the score fields, and then do another $match
pipeline operattion to returns only those documents with a score greater than 1.0. The "textScore" metadata is available for projections, sorts, and conditions subsequent the $match
stage that includes the $text
operation:
db.foods.aggregate([
{
"$match": {
"$text": {
"$search": "red blue green"
}
}
},
{
"$project": {
"_id": 0,
"score": {
"$meta": "textScore"
}
}
},
{
"$match": {
"score": { "$gt": 1.0 }
}
}
])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With