Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

294
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda