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

292
Vistas
How can i return only the value with mongoose populate?

I'm using mongoose-paginate-v2 and also i'm using populate. It works but when i populate my field, the population returns an array and i want to put the name of the value inside of the field cajaID:

What i want to get:

{
    "_id": "5aa23958-06a9-4ff1-b614-d112d81b2eSi",
    "nombre": "Zone",
    "cajaID": "Caja grande"
}

What i get:

{
    "_id": "5aa23958-06a9-4ff1-b614-d112d81b2eSi",
    "nombre": "Zone",
    "cajaID": {
        "nombre": "Caja grande"
    }
}

My code:

await ZonesModel.paginate(query, { populate: {path: 'cajaID', select: { '_id': 0,'nombre':1}}}, function(err, doc) {

    if (err) {
        error = new Error(err);
        error.status = 500;
        throw error;
    }
    
    data = doc;
})

mongoose-paginate-v2 also has projection but when i want to access to the field "nombre" it doesn't work, just returns the value of the field which has the id of the another collection.

await model.paginate(query, { populate: {path: 'cajaID', select: { '_id': 0,'nombre':1}}, projection: {  caja: "$cajaID" }}, function(err, doc) {

    if (err) {
        error = new Error(err);
        error.status = 500;
        throw error;
    }
    
    data = doc;
})

This code return this:

{
    "_id": "5aa23958-06a9-4ff1-b614-d112d81b2eSi",
    "nombre": "Zone",
    "cajaID": {
        "nombre": "Caja grande"
    },
    "caja": "5aa23958-06a9-4ff1-b614-d112d81b2eBa" // id of the another collection but i can't access to the nombre field
}
about 4 years ago · Juan Pablo Isaza
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