MongoDB: error with $ divide and $ multiply

I am creating a MongoDB aggregation pipeline and I am stuck at this point:

$group: { _id: {checkType: "$_id.checkType", resultCode: "$_id.resultCode"}, count: { $sum: "$count" }, ctv: { $sum: "$ctv" }, perc:{$multiply:[{$divide:["$ctv","$count"]},100]}, weight: { $divide: [ "$ctv", "$count"] }, details: { $push: "$$ROOT" } } 

It gives the error "Drive $ multiply is a unary operator." Similarly, if I delete a line with $ multiply, I get: "Battery $ divide is a unary operator" on the next line. I can not find a description of this error on the network. What is wrong with my syntax?

+5
source share
1 answer

Arithmetic operators cannot be used as accumulators . Move them to another stage of $project as:

 db.collection.aggregate([ { "$group": { "_id": { "checkType": "$_id.checkType", "resultCode": "$_id.resultCode" }, "count": { "$sum": "$count" }, "ctv": { "$sum": "$ctv" }, "details": { "$push": "$$ROOT" } } }, { "$project": { "count": 1, "details": 1, "ctv": 1, "perc": { "$multiply": [ { "$divide": ["$ctv","$count"] }, 100 ] }, "weight": { "$divide": ["$ctv", "$count"] }, } } ]) 
+10
source

Source: https://habr.com/ru/post/1266576/


All Articles