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.
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.