[SERVER-13567] Changed behavior of $eq with $sum in aggregation Created: 13/Apr/14  Updated: 10/Dec/14  Resolved: 14/Apr/14

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

Type: Bug Priority: Major - P3
Reporter: Neil Lunn Assignee: Unassigned
Resolution: Cannot Reproduce Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Operating System: ALL
Participants:

 Description   

In releases prior to 2.6 the following statement would work

    "read": { "$sum": { "$eq": [ "$read", true ] } }

Where essentially the "$eq" operation evaluated to true or false and was included in the $sum as either 1 or 0.

This is always evaluating to 0 when applied to the $sum in 2.6 and now requires wrapping with a $cond operator to produce the desired result:

    "read": { "$sum": { "$cond": [ "$read", 1, 0 ] } }

So I am wondering if this is by design or a bug. It does not seem to be documented as a changed behavior



 Comments   
Comment by J Rassi [ 14/Apr/14 ]

Resolving as "cannot reproduce".

Comment by Neil Lunn [ 14/Apr/14 ]

Though I would have been sure I did have this form of result somewhere I also cannot reproduce by installing different prior versions as I did with a check of this today.

So unless someone else can produce an example then I would be happy for the close.

Comment by J Rassi [ 13/Apr/14 ]

In releases prior to 2.6 the following statement would work

"read": { "$sum": { "$eq": [ "$read", true ] } }

Where essentially the "$eq" operation evaluated to true or false and was included in the $sum as either 1 or 0.

neillunn: I can't reproduce this behavior in 2.2 or 2.4. See the following shell session from 2.4.10:

> db.foo.insert({read:true})
WriteResult({ "nInserted" : 1 })
> db.foo.find()
{ "_id" : ObjectId("534ad9fae209ec7c576b7e67"), "read" : true }
> db.foo.aggregate({$group:{_id:"x",read:{$sum:{$eq:["$read",true]}}}})
{ "_id" : "x", "read" : 0 } // Are you expecting "read" to have a value of 1 here?

Unless I'm missing something, the $sum operator has always added treated boolean values as zero with respect to sum calculation. Could you specify which pre-2.6 version you were using, and give an end-to-end example which exhibits the "working" behavior you're describing with that version?

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