Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

217
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda