Reputation: 141
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?
Upvotes: 2
Views: 85
Reputation: 10525
Using pipelined update,
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
]
}
]
}
}
}
]
}
}
}
]);
Updated for multiple fields.
db.collection.update({
user: "user", "meals.name": "extraMealName"
},
[
{
$addFields: {
reducedValues: {
$reduce: {
input: "$meals",
initialValue: {
calories: 0, fat: 0
},
in: {
calories: {
$add: [
"$$value.calories",
{
$cond: [
{$eq: ["$$this.name", "extraMealName"]},
"$$this.calories",
0
]
}
]
},
fat: {
$add: [
"$$value.fat",
{
$cond: [
{$eq: ["$$this.name", "extraMealName"]},
"$$this.fat",
0
]
}
]
}
}
}
}
}
},
{
$set: {
"calories": {
$subtract: ["$calories", "$reducedValues.calories"]
},
"fat": {
$subtract: ["$fat", "$reducedValues.fat"]
},
}
}
]);
Upvotes: 1
Reputation: 91
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);
}
);
Upvotes: 0