Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

208
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!