Home > Enterprise >  How to add multiple terms to an if @cond in mongo db?
How to add multiple terms to an if @cond in mongo db?

Time:02-17

I'm going to ask probably a simple question but I cannot find any solution in the internet. I have the following mongo db query. I want the field "iocScore" to be set according to a certain logic. if the field "$score" equal to 0, set the field "iocScore" to 1, else set the field "iocScore" with the value of "$score" multiple by 10. I was able to that with the query below

"iocScore" : {
      "$cond" : {
        "if" : {
          "$eq" : ["$score", 0]
        },
        "then" : 1,
        "else" : {
          "$multiply" : ["$score", 10]
        }
      }
    }

but now I want to expend the term of the if and ask if the "$score" field equals 0 or 0.0 or null then set the "iocScore" to 1. I tried to this it like this:

  "iocScore" : {
      "$cond" : {
        "if" : {
          "$score" : {
            "$in" : [0, 0.0, null]
          }
        }
      },
      "then" : 1,
      "else" : {
        "$multiply" : ["$score", 10]
      }
    }

but I keep getting a syntax error. any idea?

CodePudding user response:

[] defines an array. Elements within the array should be the same type.

Join the three eq expressions with $or operator.

  • Related