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

$group by Objects (dictionary) is not reliable

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: 3.2.7, 3.4.5
    • Component/s: Aggregation Framework
    • Labels:
      None
    • ALL
    • Hide

      Create a test collection :

      /* 1 */
      {
          "_id" : ObjectId("594109ada25deacc151b3904"),
          "backends" : [ 
              {
                  "port" : 7080,
                  "host" : "127.0.0.1"
              }, 
              {
                  "host" : "127.0.0.1",
                  "port" : 7080
              }, 
              {
                  "port" : 7080,
                  "host" : "127.0.0.1"
              }
          ]
      }
      

      Execute the aggregate as below :

      db.getCollection('test').aggregate([
      {'$unwind': '$backends'},
      {'$group': {'_id': '$backends'}}
      ])
      

      Results, two documents:

      /* 1 */
      {
          "_id" : {
              "host" : "127.0.0.1",
              "port" : 7080
          }
      }
      
      /* 2 */
      {
          "_id" : {
              "port" : 7080,
              "host" : "127.0.0.1"
          }
      }
      

      Expected results, one document :

      /* 1 */
      {
          "_id" : {
              "host" : "127.0.0.1",
              "port" : 7080
          }
      }
      
      Show
      Create a test collection : /* 1 */ { "_id" : ObjectId( "594109ada25deacc151b3904" ), "backends" : [ { "port" : 7080, "host" : "127.0.0.1" }, { "host" : "127.0.0.1" , "port" : 7080 }, { "port" : 7080, "host" : "127.0.0.1" } ] } Execute the aggregate as below : db.getCollection( 'test' ).aggregate([ { '$unwind' : '$backends' }, { '$group' : { '_id' : '$backends' }} ]) Results, two documents: /* 1 */ { "_id" : { "host" : "127.0.0.1" , "port" : 7080 } } /* 2 */ { "_id" : { "port" : 7080, "host" : "127.0.0.1" } } Expected results, one document : /* 1 */ { "_id" : { "host" : "127.0.0.1" , "port" : 7080 } }

      Hi,
      Until now, I was doing aggregate and was implementing sort-of DISTINCT by using $group on several documents. It works fine until one of the document got rewritten with its field in a different order.
      Consequence of it, with $foo as an object, you can't assume

      $group: {'_id': '$foo'}

      is reliable.
      If it's the normal behavior, could we mention this in the docs?
      Thanks in advance

            Assignee:
            mark.agarunov Mark Agarunov
            Reporter:
            JonathanHuot Jonathan Huot [X]
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved: