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

151
Views
Optimize mongo aggregation with match and sort

I've an aggregation pipeline that is getting quite slow. Let's say my documents looks like:

{
  value1,
  value2,
}

and that they are indexed on both fields.

My pipeline is:

collection.aggregate([
  {
    $match: { value1: someVar },
  },
  {
    $sort: { value2: 1 },
  },
  {
    $limit: 5,
  },
], cb);

The issue is that there's quite a few documents matching (~10k) and that I'm losing the index advantage on the second step of the pipeline. Is there a good way to improve that that would leverage the indexing for both match and sort?

over 4 years ago · Santiago Trujillo
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!