Reputation: 206
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?
Upvotes: 0
Views: 34
Reputation: 16033
One option is to replace the $set
with $inc
for this:
await single_sku_db.collection("test").updateOne(
{ _id: ObjectId(id) },
{
$push: {
report: {
$each: [
{
name: name,
views,
},
],
$position: 0,
},
},
$set: {report_status: "Completed"},
$inc: {total_views: views},
})
See how it works on the playground example
Upvotes: 2