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

Grouping/accumulating with a switch expression doesn't yield the expected result

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 4.2.1
    • Component/s: Aggregation Framework
    • None
    • Environment:
      Arch Linux
    • ALL
    • Hide

      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 }
      Show
      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 }

      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?

            Assignee:
            Unassigned Unassigned
            Reporter:
            alien Boris Petrov
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: