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
after your schema definition you can try to add this line
PostSchema .index({ workSpace: 1 });