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

Why we can not push not push the data from top in $group aggreagtion

    • Type: Icon: Question Question
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 4.3.2
    • Component/s: Aggregation Framework
    • Labels:
      None

       

      data = [
      {"name": a, dept: "IT", score: 150},
      {"name": b, dept: "IT", score: 100},
      {"name": c, dept: "Sales", score: 260},
      {"name": d, dept: "Sales", score: 200}
      ]

      Now we run following aggregation

       

       

      db.serviceoffering.aggregate([
          {$sort: {score: 1}},
          {$group: {_id: {dept: "$dept"}, user: {$push: "$$ROOT"}}}
      ])
      

       

      Now after $sort pipeline the result would be following:

       

       

      data = [
      {"name": b, dept: "IT", score: 100},
      {"name": a, dept: "IT", score: 150},
      {"name": d, dept: "Sales", score: 200},
      {"name": c, dept: "Sales", score: 260}
      ]
      

       

      But after $group pipeline result becomes:

      {
      {
      _id: IT,
      user:[
        {"name": d, dept: "Sales", score: 200}, 
        {"name": c, dept: "Sales", score: 260} 
      ]
      },
      {
      _id: Sales,
      user:
        [
          {"name": b, dept: "IT", score: 100}, 
          {"name": a, dept: "IT", score: 150}
        ]
      }
      }

      If you observe the result data has been taken from bottom and hence even after sort d,c are on top in the final result.

       

            Assignee:
            eric.sedor@mongodb.com Eric Sedor
            Reporter:
            saubhik.singh@gslab.com Saubhik Singh
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: