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

218
Visualizações
How can I check if there is a value in a MongoDB object

I have my users in MongoDB that I identify with "nir" and "sN" my goal is to locate the user with nir 33 and sN 1234, once the user is located, see if "sR" has a "local" or "mobile" value, depending on whether it has the value "local" or "mobile" it sends to the console: isLocal: true, isMobile: false.

{nir: 33, sN: 1234 ,sR: "local"},
{nir: 33, sN: 4545 ,sR: "local"},
{nir: 81, sN: 2362 ,sR: "mobile"},

Thanks.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can simply do a find, and use the projection to wrangle the 2 flags.

db.collection.find({
  // input your query here
  nir: 33,
  sN: 1234
},
{
  isLocal: {
    "$cond": {
      "if": {
        $ne: [
          {
            "$indexOfCP": [
              "$sR",
              "local"
            ]
          },
          -1
        ]
      },
      "then": true,
      "else": false
    }
  },
  isMobile: {
    "$cond": {
      "if": {
        $ne: [
          {
            "$indexOfCP": [
              "$sR",
              "mobile"
            ]
          },
          -1
        ]
      },
      "then": true,
      "else": false
    }
  }
})

Here is the Mongo playground for your reference.

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