-
Type: Bug
-
Resolution: Duplicate
-
Priority: Major - P3
-
None
-
Affects Version/s: 2.5.2
-
Component/s: Aggregation Framework
-
None
-
ALL
In the following example, I'm attempting to compute the product of all x fields in a collection. After the $group step, vals would be an array of these values. I would expect to be able to pass vals directly to $multiply and have it compute the product; however, $multiply instead considers the de-referenced value as an argument to be multiplied instead of the argument array.
I imagine this is related to $multiply supporting a scalar argument, in which case it returns that numeric value * 1 (and complains if the value is not numeric).
> db.foo.find() { "_id" : ObjectId("522784a423582cd7f71acca7"), "x" : 2 } { "_id" : ObjectId("522784a523582cd7f71acca8"), "x" : 3 } { "_id" : ObjectId("522784a623582cd7f71acca9"), "x" : 4 } > pipeline [ { "$group" : { "_id" : 1, "vals" : { "$push" : "$x" } } }, { "$project" : { "product" : { "$multiply" : "$vals" } } } ] > db.foo.aggregate(pipeline) Error: Printing Stack Trace at printStackTrace (src/mongo/shell/utils.js:37:15) at DBCollection.aggregate (src/mongo/shell/collection.js:897:9) at (shell):1:8 Wed Sep 4 15:07:05.712 JavaScript execution failed: aggregate failed: { "errmsg" : "exception: $multiply only supports numeric types, not Array", "code" : 16555, "ok" : 0 } at src/mongo/shell/collection.js:L898
Here's an example of an array value being passed as an argument to be multiplied (using all literals):
> db.foo.aggregate({$project:{p:{$multiply:[[3]]}}}) Error: Printing Stack Trace at printStackTrace (src/mongo/shell/utils.js:37:15) at DBCollection.aggregate (src/mongo/shell/collection.js:897:9) at (shell):1:8 Wed Sep 4 15:12:32.572 JavaScript execution failed: aggregate failed: { "errmsg" : "exception: $multiply only supports numeric types, not Array", "code" : 16555, "ok" : 0 } at src/mongo/shell/collection.js:L898
- duplicates
-
SERVER-10676 $multiply does not support a reference to an array
- Closed
- is related to
-
SERVER-10682 Expose $multiply as both an accumulator and an expression
- Backlog
- related to
-
SERVER-31991 Allow n-ary aggregation expressions to compute their array of arguments dynamically
- Backlog