Updating data with $set and $sum

I was trying to do update and sum up the column value.

await single_sku_db.collection("test").updateOne(
      { _id: ObjectId(id) },
      {
        $push: {
          report: {
            $each: [
              {
                name: name,
                views,
              },
            ],
            $position: 0,
          },
        },
        $set: {
          report_status: "Completed",
          total_views: { $sum: "$report.views"},
        },
      }

I cant sum the report.views like this, will get this error.

the dollar ($) prefixed field ‘’ is not valid for storage.

Is there anyway to do this without using aggregate?

You cannot refer to other fields of the document without the aggregation syntax.

But since you already have views as a variable, why don’t you simply do

Is there any reason why you use inconsistent syntax for

and

2 Likes