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

103
Views
MongoDB index on ref is not taking effect

I have the following model

const PostSchema = new mongoose.Schema(
    {
        _id: mongoose.Schema.Types.ObjectId,
        workSpace: {
            type: mongoose.Schema.Types.ObjectId,
            ref: 'Workspace'
        }
    }
);

After getting complains of slow requests, I went over to my MongoDB Atlas dashboard in production and created an index on the Post documents like this

{
  'workSpace':1
}

Yet, when I queried for posts like this

let skip=userDefined ?? 0;
let limit=1000;

let postsInWorkSpace = await Post.find({workSpace})
            .sort({ created_at: 'desc' })
            .skip(limit * page)
            .limit(limit);

The above query still takes up to 50seconds before returning results.

So, is there something else I need to do to make the above query faster, because right now it seems the index is not working?

Thank you

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

after your schema definition you can try to add this line

PostSchema .index({ workSpace: 1 });
about 4 years ago · Juan Pablo Isaza 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!