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

Unable to access _id subdocuments in $group

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 2.2.1
    • Component/s: Aggregation Framework
    • None
    • Environment:
      amd64 Amazon Linux AMI 2012.09 with amd64 mongo installed from 10gen yum repository.
    • Fully Compatible
    • ALL
    • Hide

      Create a collection that has a compound _id field. For example {_id:

      {a: 1, b:2}

      , c :3} then do an aggregation with a $group where _id references _id.a. For example {"$group" : {_id : "$_id.a" , total:

      {"$sum" : "$c" }

      }}. In this case _id will be end up as null as _id.a is ignored.

      Show
      Create a collection that has a compound _id field. For example {_id: {a: 1, b:2} , c :3} then do an aggregation with a $group where _id references _id.a. For example {"$group" : {_id : "$_id.a" , total: {"$sum" : "$c" } }}. In this case _id will be end up as null as _id.a is ignored.

      It appears that if one is using the Aggregation Framework on a collection where _id is an object with subfields, that $group will ignore anything that is a subfield of _id when specifying it's own _id. The main case where this will occur is in using the aggregation framework on a collection that is the output of a map reduce. It is possible to work around this by using a $project stage to rename the fields from _id.Z to Z for instance. However this presumably takes extra resources. There is also no error message which is likely to cause frustration to the developer or user.

      For example the following two aggregations should in principle be identical with the project phase in the second being superfluous. The projection of value.I to I and value.U to U appear to be unnecessary as the problem seems specific to _id.
      mongos> db.hourAggregate.aggregate( { $match : { "value.U" : 158, "_id.t" :"I"}} , {$group : {_id :

      {Z: "$_id.Z", U : "$value.U" }

      , count : {$sum: "$value.I"} }})
      {
      "result" : [
      {
      "_id" :

      { "U" : 158 }

      ,
      "count" : NumberLong(1615478)
      }
      ],
      "ok" : 1

      }
      mongos> db.hourAggregate.aggregate( { $match : { "value.U" : 158, "_id.t" :"I"}} , {$project : { Z:"$_id.Z", U:"$value.U", I : "$value.I"}} ,{$group : {_id :

      {Z: "$Z", U : "$U" }

      , count : {$sum: "$I"} }})
      {
      "result" : [
      {
      "_id" :

      { "Z" : 137, "U" : 158 }

      ,
      "count" : NumberLong(541555)
      },
      {
      "_id" :

      { "Z" : 138, "U" : 158 }

      ,
      "count" : NumberLong(470692)
      },
      {
      "_id" :

      { "Z" : 139, "U" : 158 }

      ,
      "count" : NumberLong(603231)
      }
      ],
      "ok" : 1

      See https://groups.google.com/d/topic/mongodb-user/Yw3fvn7udY4/discussion

            Assignee:
            mathias@mongodb.com Mathias Stearn
            Reporter:
            cacsar Christian Csar
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: