Home > database >  MongoDB/Mongoose : CastError in updateMany $inc
MongoDB/Mongoose : CastError in updateMany $inc

Time:01-06

My MongoDB schema (simplified):

user: ObjectID
calories: Number
meals:[{
    calories: Number
    name:String
}]

And I have a updateMany query:

await Meals.updateMany(
  { user: user, 'meals.name': extraMealName },
  { $inc: { calories: 'meals.$.calories' } },
  {multi : true},  
  function(error, result) {
    console.log(error);
  }
);

The query throws me this error:

CastError: Cast to Number failed for value "meals.$.calories" at path "calories"

I have tried changing the query for the last hour, but nothing worked... I also browsed stackoverflow, but found nothing I could work with

Does someone have an idea how to fix this?

CodePudding user response:

Using pipelined update,

  • $reduce, go through the meals array and add up the calories where name=extraMealName
  • $subtract from calories, the sum from previous step

mongoplayground

db.Meals.update({
  user: "user", "meals.name": "extraMealName"
},
[
  {
    $set: {
      calories: {
        $subtract: [
          "$calories",
          {
            $reduce: {
              input: "$meals",
              initialValue: 0,
              in: {
                $add: [
                  "$$value",
                  {
                    $cond: [
                      {$eq: ["$$this.name", "extraMealName"]},
                      "$$this.calories",
                      0
                    ]
                  }
                ]
              }
            }
          }
        ]
      }
    }
  }
]);

CodePudding user response:

the $inc has a syntax error, $inc expects a number not string so try some like this.

await Meals.updateMany(
  { user: user, 'meals.name': extraMealName },
  { $inc: { calories: { $sum: '$meals.$.calories' } } },
  { multi: true },
  function(error, result) {
    console.log(error);
  }
);
  • Related