[SERVER-24021] Shouldn't {$sum:1} inside a $group aggregation step give a "long" not "double" ? Created: 03/May/16  Updated: 03/May/16  Resolved: 03/May/16

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

Type: Question Priority: Minor - P4
Reporter: Nic Cottrell (Personal) Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Participants:

 Description   

In this step:

     {
        "$group": {
          "_id": {"tLc": "$indices", lc: "$lc", group: "$group"},
          "f": {"$sum": 1}
        }
      },

the field "f" is output as a Double, but since it's summing an integer, wouldn't a Long be more appropriate. Is there any way to cast inside the AF?



 Comments   
Comment by Charlie Swanson [ 03/May/16 ]

Hi niccottrell,

The $sum accumulator will preserve the 'widest' type it sees:

> db.foo.drop()
true
> db.foo.insert({_id: 0, x: NumberLong(1)})
WriteResult({ "nInserted" : 1 })
> db.foo.insert({_id: 1, x: NumberLong(1)})
WriteResult({ "nInserted" : 1 })
> db.foo.insert({_id: 2, x: 1})  // This is a double.
WriteResult({ "nInserted" : 1 })
> db.foo.find()
{ "_id" : 0, "x" : NumberLong(1) }
{ "_id" : 1, "x" : NumberLong(1) }
{ "_id" : 2, "x" : 1 }
> db.foo.aggregate([{$match: {_id: {$lte: 1}}}, {$group: {_id: null, sum: {$sum: "$x"}}}])
{ "_id" : null, "sum" : NumberLong(2) }  // All inputs were 64-bit integers, so return type is 64-bit int.
> db.foo.aggregate([{$match: {_id: {$lte: 2}}}, {$group: {_id: null, sum: {$sum: "$x"}}}])
{ "_id" : null, "sum" : 3 }  // One input was a double, so result is a double.

To be more specific, the return type will be a 32-bit integer if all inputs were 32-bit integers, a 64-bit integer if all inputs were 32-bit or 64-bit integers, or a double if any input was a double.

Hope this helps,
-Charlie

Comment by Nic Cottrell (Personal) [ 03/May/16 ]

This is an artifact of how the aggregation framework handles the $sum command, surely? I don't see how it's got anything to do with the Javascript console (It's the same if I call from the Java driver). I really assumed that the $sum with an integer with keep the 32- or 64-bit Int BSON type.

Comment by Ramon Fernandez Marina [ 03/May/16 ]

niccottrell, -if you're running the code above in the shell you'll get a Double because that's what JavaScript uses.-
EDIT: that response was incorrect, apologies for the confusion. See below for the right answer.

Please note that the SERVER project is for reporting bugs or feature suggestions for the MongoDB server. For MongoDB-related support discussion please post on the mongodb-user group or Stack Overflow with the mongodb tag, where your question will reach a larger audience. A question like this involving more discussion would be best posted on the mongodb-user group.

Regards,
Ramón.

Generated at Thu Feb 08 04:05:09 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.