Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

93
Vistas
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 Respuestas
Responde la pregunta

0

after your schema definition you can try to add this line

PostSchema .index({ workSpace: 1 });
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda