Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-10681

$multiply operator cannot processed a referenced array field

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 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
      

            Assignee:
            matt.dannenberg Matt Dannenberg
            Reporter:
            jmikola@mongodb.com Jeremy Mikola
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: