Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

179
Views
MongoDB Atlas search: sort by "searchScore"

I run the following aggregation pipeline and it works well:

[
  {
    $search: {
      text: {
        query: text,
        path: 'name',
        fuzzy: {
          maxEdits: 2,
          prefixLength: 0,
          maxExpansions: 256,
        },
      },
    },
  },
  {
    $limit: 10,
  },
  {
    $project: {
      _id: 1,
      name: 1,
      score: { $meta: 'searchScore' },
     },
  },
]

I can see that the score-field is present in my result and is correct. Now I want to sort the documents retrieved by the $meta-property searchScore. However, adding this step at the end of the pipeline:

{
  $sort: { score: { $meta: 'searchScore' }, _id: 1 },
}

Yields the following error:

MongoError: $meta sort by 'searchScore' metadata is not supported

How do I achieve this?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You need to add a new field that contains the result of $meta before sorting (which you already do) and then sort on that field, so you new pipeline would be:

...

{
    $project: {
      _id: 1,
      name: 1,
      score: { $meta: 'searchScore' }, // you are already adding the field here.
     },
},
{
   $sort: {
      score: -1, // use the new computed field here.
      _id: 1
   }
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!