title: Why mongo aggregate not return a consistent order?
body: <p>Here my query:</p>
  <pre><code>db.getCollection('alerts').aggregate([{
    $match: {
      houseId: ObjectId(&quot;609100a56ed9f8001351aee3&quot;)
    }
  }, {
      $group: {
          _id: '$type',
          alerts: {
              $push: {
                  _id: '$_id',
                  type: '$type'
              }
          }
       }
    },
    {
      $project: {
         _id: false,
         type: '$_id.type',
         alerts: '$alerts'
      }
  
    }
  ])
  </code></pre>
  <p>And result:</p>
  <pre><code>[{
      &quot;type&quot; : &quot;cool&quot;,
      &quot;alerts&quot; : [ 
          {
              &quot;_id&quot; : ObjectId(&quot;61387e740dc3d853f1eee5b0&quot;),
              &quot;type&quot; : &quot;cool&quot;
          }
      ]
  }, {
      &quot;type&quot; : &quot;hot&quot;,
      &quot;alerts&quot; : [ 
          {
              &quot;_id&quot; : ObjectId(&quot;61387e740dc3d853f1eee5b0&quot;),
              &quot;type&quot; : &quot;hot&quot;
          },
          {
              &quot;_id&quot; : ObjectId(&quot;61387e740dc3d853f1eee5b0&quot;),
              &quot;type&quot; : &quot;hot&quot;
          }
      ]
  }]
  </code></pre>
  <p>but sometime it also return:</p>
  <pre><code>[{
      &quot;type&quot; : &quot;hot&quot;,
      &quot;alerts&quot; : [ 
          {
              &quot;_id&quot; : ObjectId(&quot;61387e740dc3d853f1eee5b0&quot;),
              &quot;type&quot; : &quot;hot&quot;
          },
          {
              &quot;_id&quot; : ObjectId(&quot;61387e740dc3d853f1eee5b0&quot;),
              &quot;type&quot; : &quot;hot&quot;
          }
      ]
  }, {
      &quot;type&quot; : &quot;cool&quot;,
      &quot;alerts&quot; : [ 
          {
              &quot;_id&quot; : ObjectId(&quot;61387e740dc3d853f1eee5b0&quot;),
              &quot;type&quot; : &quot;cool&quot;
          }
      ]
  }]
  </code></pre>
  <p>I added a stage $limit and $skip for paging, if skip: 0, limit: 1 then the first record sometime is cool, sometime is hot. I also try to add stage $sort to make consistent order but it's not works.</p>
  <p>My script with sort and limit, skip</p>
  <pre><code>db.getCollection('alerts').aggregate([{
    $match: {
      houseId: ObjectId(&quot;609100a56ed9f8001351aee3&quot;)
    }
  }, {
      $group: {
          _id: '$type',
          alerts: {
              $push: {
                  _id: '$_id',
                  type: '$type'
              }
          }
       }
    },
    {
      $project: {
         _id: false,
         type: '$_id.type',
         alerts: '$alerts'
      }
  
    },
    {
      $limit: limit + skip,
    },
    {
      $skip: skip,
    },
    {
      $sort: { type: 1 }
    }
  ])
  </code></pre>
  <p>My expected is the result return the same order</p>