[SERVER-10681] $multiply operator cannot processed a referenced array field Created: 04/Sep/13  Updated: 30/May/19  Resolved: 04/Sep/13

Status: Closed
Project: Core Server
Component/s: Aggregation Framework
Affects Version/s: 2.5.2
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Jeremy Mikola Assignee: Matt Dannenberg
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates SERVER-10676 $multiply does not support a referenc... Closed
Related
related to SERVER-31991 Allow n-ary aggregation expressions t... Backlog
is related to SERVER-10682 Expose $multiply as both an accumulat... Backlog
Operating System: ALL
Participants:

 Description   

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


Generated at Thu Feb 08 03:23:47 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.