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

252
Visualizações
Exclude certain attributes from object using the populate from mongodb using aggregate

I want to exclude for example email and address using populate() function from mongodb, just get the name from it:

Example:

const results = await Seller.aggregate(aggregatePipeline).exec();
const sellers = await Seller.populate(results, { path: "user" });

When populating the user instead of having:

...
user: {
    email: "hgjh@gmail.com",
    address:{},
    name: "name"
}

I want to only have (exclude certain data from the path):

...
user: {
   name: "name"
}
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You can do either,

const sellers = await Seller.populate(results, { path: "user", select: '- 
email -address'  });

or

const sellers = await Seller.populate(results, { path: "user", select: 
'name'  });
over 4 years ago · Santiago Trujillo Relatório

0

As i understand mongoose documentation https://mongoosejs.com/docs/populate.html, populate as $lookup is use to resolve a relation with other collection.

MongoDB has the join-like $lookup aggregation operator in versions >= 3.2. Mongoose has a more powerful alternative called populate(), which lets you reference documents in other collections.

In your case, you don't need to resolve a field with an other collection. You already have the final data you target . You could use $project at the end of your pipeline aggregation to keep only name field, like :

{ $project: { name:1 } }

Let me know if i helped you.

Edit :

I read too fast, if you have this data res after the populate and not after the aggreg, you may select your final field, like is said here https://stackoverflow.com/a/72481338/16205278

user: {
    email: "hgjh@gmail.com",
    address:{},
    name: "name"
}
over 4 years ago · Santiago Trujillo Relatório
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