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

138
Visualizações
is there a way to find and return a property if it has a specific value?

What I want is that in users query it returns the email data whenever private is false.

this is my scheme

const userSchema = new Schema({
  email: {
    private: {
      type: Boolean,
      default: false,
    },
    data: {
      type: String,
      unique: true,
      required: true,
    },
  },
  password: {
    type: String,
    required: true,
  }
})

I tried to do it this way, but it doesn't work because it is not dynamic.

const user = await User.find({}, '-email.data')

the result I am looking for is this

[
  { _id: 0, email: { private: true } },
  { _id: 1, email: { private: false, data: 'email@email.com' } }
]
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Try $cond operator, check if email.private is true then return only private property otherwise full email object

const user = await User.find({},
{
  email: {
    $cond: [
      { $eq: ["$email.private", true] },
      { private: true },
      "$email"
    ]
  }
})

Playground

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