Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Divide by zero generates an error on mongodb aggregation

I have a dataset like this:

{
    "_id" : ObjectId("5c35f04c4e92b8337885d9a6"),
    "activity" : {
        "a1": 10,
        "a2": 11,
        "a3": 12,
        "a4": 13,
        "a5": 14,
        "b1": 5,
        "b2": 6
    }
}

NOTE: This is a dummy entries, actual Entries will be around 50 or 60 out of that 30 are from simple addition (like from a1 to a5) and 20 entries are for ratio (like b1 and b2). For asking the concept I minimize the dataset

Now from a1 to a5 I have to do addition with some weightage value of multiplication like this: lets say multiplication value is 0.5

so it will be like this:
(a1*0.5 + a2*0.5 + a3*0.5 + a4*0.5 + a5*0.5)

and then I have to take the ratio of b1 and b2 and then multiply be some weightage like this

((b1:b2)*0.5)

So final calculation will be like this:

(a1*0.5 + a2*0.5 + a3*0.5 + a4*0.5 + a5*0.5 + (b1:b2)*0.5)

For that I used mongodb aggregation. My code is this:

db.Collection.aggregate([
  { $match: { _id: ObjectId("5c35f04c4e92b8337885d9a6") } },
  { $unwind: "$_id" },
  {
    $project: {
      "activity.score": {
        $add: [
          {
            $multiply: [
              "$activity.a1",
              0.5
            ]
          },
          {
            $multiply: [
              "$activity.a2",
              0.5
            ]
          },
          {
            $multiply: [
              "$activity.a3",
              0.5
            ]
          },
          {
            $multiply: [
              "$activity.a4",
              0.5
            ]
          },
          {
            $multiply: [
              "$activity.a5",
              0.5
            ]
          },
          {
            $multiply: [
              {
                $divide: [
                  "$activity.b1",
                  "$activity.b2"
                ]
              },
              0.5
            ]
          }

        ]
      }
    }
  }
])
  .then(data => {
    console.log(data);
  })
  .catch(error => {
    console.log(error);
  });

By this I got the answer, but in case my b2 will be 0 like this:

{
    "_id" : ObjectId("5c35f04c4e92b8337885d9a6"),
    "activity" : {
        "a1": 10,
        "a2": 11,
        "a3": 12,
        "a4": 13,
        "a5": 14,
        "b1": 5,
        "b2": 0
    }
}

MongoDB generates an error i.e MongoError: can't $divide by zero. Is there anyone who can suggest me How to achieve this. Any help or suggestion is really appreciable for that. Thanks in advance for the effort and interaction.

like image 960
Ankit Avatar asked Jan 20 '26 13:01

Ankit


1 Answers

Try with this it works!

{
  "activity.score": {
    $add: [
      {
        $multiply: [
          "$activity.a1",
          0.5
        ]
      },
      {
        $multiply: [
          "$activity.a2",
          0.5
        ]
      },
      {
        $multiply: [
          "$activity.a3",
          0.5
        ]
      },
      {
        $multiply: [
          "$activity.a4",
          0.5
        ]
      },
      {
        $multiply: [
          "$activity.a5",
          0.5
        ]
      },
      {
        $multiply: [
         { $cond: [ { $eq: [ "$activity.b2", 0 ] }, 0,  {
            $divide: [
              "$activity.b1",
              "$activity.b2"
            ]
          }
       ] }
         ,
          0.5
        ]
      }
    ]
  }
}
like image 122
Kruti Choksi Patel Avatar answered Jan 23 '26 16:01

Kruti Choksi Patel