If-else conditional in $set operator

I went through many other posts but none of the solutions worked for me. I asked the following question in reply to someone’s post but couldn’t sort out the issue so now I am posting it as a separate topic.
So, without going into other details of my project, I have a collection containing multiple documents. In each document, there is an array named “team” and inside this array, I have objects. Each individual object contain information for a particular player like name, points, _id and isCaptain (which tells that whether this particular player is a captain of this team or not in the form of ‘true’ and ‘false’ i.e isCaptain: true means that this player is captain of this team and isCaptain: false means that this player is not a captain of this team). From the frontend, I am sending two values; id and points. Now, what I am doing is that I’m fetching/reaching out to all the players(objects) whose _id matches with the input ‘_id’ (coming from the frontend). After this, I want to check if this player is the captain of this team or not. If he is, then I want to $set the points of this player as double points by doing points 2x. And if that player is not a captain, then I just want to $set points to the input points without making it double.*

*I was able to do everything but I’m stuck on isCaptain. I want my code to be modelled in such a way that it checks the matching _id and also check that if that player is captain or not. If he is captain than double the points (points 2x) otherwise go to the other update operation. I have attached the code snippet plus the Screenshots of my documents.

Let me know if I was able to make it clear enough for you to understand. If you need more details, I can provide you with that.

1 Like

Hi @Najam_Ul_Sehar,

You can do it with single update query like this:

  • $set - to update points property.
  • $cond with $eq - to check if player is isCaptain property is equal to true.
  • $multiply - to multiply input points by 2 if player is captain.
db.collection.update({
  _id: 1
},
[
  {
    "$set": {
      "points": {
        "$cond": {
          "if": {
            "$eq": [
              "$isCaptain",
              true
            ]
          },
          "then": {
            "$multiply": [
              100,
              2
            ]
          },
          "else": 100
        }
      }
    }
  }
])

Working example

4 Likes

Hi NeNaD,
First of all, thank you for reaching out and helping out.
I copied your code and tested it with my collection but it didn’t work.
I have documents like following:

And my team array look like following:

So, my goal is to update the “points” property of individual matching objects (players).
Firstly, does it really matter that we put “” marks around $set or $cond etc? Secondly, I have an array with multiple objects (objects means each individual players) and hence I am doing “team.$.isCaptain” to access each player’s captain property, that’s why I modified the code in the following way but it didn’t work either.


 MyTeam.updateMany({ matchid: matchid, 'team._id': id },
        {
            "$cond": {
                "if": {
                  "$eq": [
                    "team.$.isCaptain",
                    true
                  ]
                },
                "then": {
                    "$set": {
                        "team.$.points": points*2
                    }
                //   "$multiply": [
                //     points,
                //     2
                //   ]
                },
                "else": {
                    "$set": {
                        "team.$.points": points
                    }
                }
              }
        },(err, numAffected) => {
            if(err) throw err;
            res.send(numAffected);
        }
)

Let me know what I’m doing wrong here.