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

Can't access specific array elements in $group

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: Aggregation Framework
    • None
    • Fully Compatible
    • ALL

      It seems not possible to access a specific array element in the $group stage. For instance, let's say my documents have 2 arrays, I want to unwind the first array and group by the campaign_id and day pair. I also want to access a certain array element for some reason, but I can not.

      mongos> db.test.find()
      { "_id" : ObjectId("548608660094d27682c03666"), "campaign_id" : 1, "channel1" : [ { "day" : 0, "delivered" : 10 }, { "day" : 1, "delivered" : 1 } ], "channel2" : [ { "day" : 0, "delivered" : 5 }, { "day" : 1, "delivered" : 6 } ] }
      { "_id" : ObjectId("548608a20094d27682c03667"), "campaign_id" : 2, "channel1" : [ { "day" : 0, "delivered" : 1 }, { "day" : 1, "delivered" : 1 } ], "channel2" : [ { "day" : 0, "delivered" : 5 }, { "day" : 1, "delivered" : 5 } ], "channel3" : [ { "day" : 0, "delivered" : 11 }, { "day" : 1, "delivered" : 1 } ] }
      
      mongos> db.test.aggregate({$unwind : "$channel1"},{$group : {_id : {campaign_id : "$campaign_id", day : "$channel1.day"}, xxx: {$first : "$channel2"} }})
      { "_id" : { "campaign_id" : 2, "day" : 1 }, "xxx" : [ { "day" : 0, "delivered" : 5 }, { "day" : 1, "delivered" : 5 } ] }
      { "_id" : { "campaign_id" : 1, "day" : 1 }, "xxx" : [ { "day" : 0, "delivered" : 5 }, { "day" : 1, "delivered" : 6 } ] }
      { "_id" : { "campaign_id" : 2, "day" : 0 }, "xxx" : [ { "day" : 0, "delivered" : 5 }, { "day" : 1, "delivered" : 5 } ] }
      { "_id" : { "campaign_id" : 1, "day" : 0 }, "xxx" : [ { "day" : 0, "delivered" : 5 }, { "day" : 1, "delivered" : 6 } ] }
      mongos> db.test.aggregate({$unwind : "$channel1"},{$group : {_id : {campaign_id : "$campaign_id", day : "$channel1.day"}, xxx: {$first : "$channel2.0.day"} }})
      { "_id" : { "campaign_id" : 2, "day" : 1 }, "xxx" : [ ] }
      { "_id" : { "campaign_id" : 1, "day" : 1 }, "xxx" : [ ] }
      { "_id" : { "campaign_id" : 2, "day" : 0 }, "xxx" : [ ] }
      { "_id" : { "campaign_id" : 1, "day" : 0 }, "xxx" : [ ] }
      mongos> db.test.aggregate({$unwind : "$channel1"},{$group : {_id : {campaign_id : "$campaign_id", day : "$channel1.day"}, xxx: {$first : "$channel2.0"} }})
      { "_id" : { "campaign_id" : 2, "day" : 1 }, "xxx" : [ ] }
      { "_id" : { "campaign_id" : 1, "day" : 1 }, "xxx" : [ ] }
      { "_id" : { "campaign_id" : 2, "day" : 0 }, "xxx" : [ ] }
      { "_id" : { "campaign_id" : 1, "day" : 0 }, "xxx" : [ ] }
      mongos> db.test.aggregate({$unwind : "$channel1"},{$group : {_id : {campaign_id : "$campaign_id", day : "$channel1.day"}, xxx: {$first : "$channel2[0]"} }})
      { "_id" : { "campaign_id" : 2, "day" : 1 }, "xxx" : null }
      { "_id" : { "campaign_id" : 1, "day" : 1 }, "xxx" : null }
      { "_id" : { "campaign_id" : 2, "day" : 0 }, "xxx" : null }
      { "_id" : { "campaign_id" : 1, "day" : 0 }, "xxx" : null }
      

            Assignee:
            Unassigned Unassigned
            Reporter:
            alex.komyagin@mongodb.com Alexander Komyagin (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved: