[SERVER-45016] Grouping/accumulating with a switch expression doesn't yield the expected result Created: 08/Dec/19  Updated: 09/Dec/19  Resolved: 09/Dec/19

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

Type: Bug Priority: Major - P3
Reporter: Boris Petrov Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Arch Linux


Operating System: ALL
Steps To Reproduce:

db.test.insert({ a: 1 })
db.test.insert({ a:

{ b: 2 }

})
db.test.insert({ a: { b:

{ c: 3 }

} })

db.test.aggregate([{ $group: { _id: null, sum: { $sum: { $switch: { branches: [{ case:

{ $ne: ['$a.b.c', null] }

, then: '$a.b.c' }, { case:

{ $ne: ['$a.b', null] }

, then: '$a.b' }, { case:

{ $ne: ['$a', null] }

, then: '$a' }] } } } } }])

The expected result would be `6`, but instead I get:

{ "_id" : null, "sum" : 3 }
Participants:

 Description   

I'm not exactly sure this isn't by definition but it definitely doesn't make sense and I haven't seen it in the documentation. Please check the example in the "Steps to Reproduce". The same is true for the `_id` field - when using `switch` - only one of the values is used (perhaps the "longest"?) while I would expect each document to be evaluated separately on that switch. Am I missing something? How am I supposed to do this correctly or is this really a bug as I think?



 Comments   
Comment by Carl Champain (Inactive) [ 09/Dec/19 ]

Thank you alien. I'm going to close this ticket then.

Comment by Boris Petrov [ 09/Dec/19 ]

Actually I now see that changing `null` to `undefined` fixes the issue. I should have thought that checks in Mongo are done by using `===` and not `==`. This issue can be closed.

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