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

305
Visualizações
Is it possible to query a element in mongoose map by key alone

If I have a mongoose schema with a map. Is it possible to query a element in the map by key alone

const userSchema = new Schema( {
    
    socialHandles: {
        type: Map,
        of: String
    },
    active: {type:Boolean, default:true}
} );

I am able to query and find a user whose social handles has instagram key and instagram key holds a particular value with the below syntax.

let user = await User.findOne({ 'socialHandles.instagram': 'jondoe' });

I am looking for a way to query and find the instagram ids of user if a key called instagram exists. Something of the below form ie get a user with a social handle instagram ( I would need the value of instagram later on).

let user = await User.findOne({ 'socialHandles.instagram': * });  // this is just a wrong syntax to explain what I want to achieve
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can use $exists like this:

User.findOne({
  "socialHandles.instagram": {
    "$exists": true
  }
})

Example here

about 4 years ago · Juan Pablo Isaza Relatório

0

Use exists

let user = await User.findOne().exists('socialHandles.instagram')
about 4 years ago · Juan Pablo Isaza 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