title: How to take count of status field from mongo db collection in laravel eloquent raw method?
body: <p>How to write group by and count status in laravel eloquent raw method?
  I am using mongodb.</p>
  
  <p>For testing purpose I took the count using mysql (result and query given below) but I don't know how to take count in mongodb.</p>
  
  <p>Please check collections and code given below. Thanks</p>
  
  <p><strong>SQL</strong></p>
  
  <p><code>SELECT count(*), status, date FROM booking WHERE date BETWEEN '2017-08-01' AND '2017-08-31' group by status, date order by date</code></p>
  
  <p><strong>Results</strong></p>
  
  <p><code>count status date
   3      1    2017-08-01
   1      2    2017-08-01
   2      2    2017-08-02
   1      3    2017-08-02
   1      1    2017-08-03
   1      2    2017-08-03
   1      1    2017-08-04
   1      2    2017-08-04
   1      3    2017-08-04
   1      3    2017-08-05
   2      2    2017-08-06
   2      3    2017-08-07
   1      1    2017-08-08
   1      2    2017-08-08
   1      3    2017-08-08
   1      1    2017-08-09
   1      2    2017-08-09</code></p>
  
  <p><strong>Controller.php</strong></p>
  
  <pre><code>$bookings               = Booking::raw(function ($collection) use ($cabinName, $dateBegin, $dateEnd) {
                  return $collection-&gt;aggregate([
                      [
                          '$match' =&gt; [
                              'is_delete' =&gt; 0,
                              'cabinname' =&gt; $cabinName,
                              'checkin_from' =&gt; ['$gte' =&gt; $dateBegin, '$lte' =&gt; $dateEnd]
                          ],
                      ],
                      [
                          '$group' =&gt;
                              [
                                  '_id' =&gt; ['checkin_from' =&gt; '$checkin_from','cabinname' =&gt; '$cabinname'],
  
                              ],
                      ],
                      [
                          '$project' =&gt;
                              [
                                  'checkin_from' =&gt; '$_id.checkin_from',
                                  'cabinname' =&gt; '$_id.cabinname',
  
                              ],
                      ],
                      [
                          '$sort' =&gt;
                              [
                                  'checkin_from' =&gt; 1
                              ],
                      ],
                  ]);
              });
  </code></pre>
  
  <p><strong>Mongo collection</strong></p>
  
  <pre><code>{ "_id" : ObjectId("598c07a4f2017f945660bc1d"), "cabinname" : "Kemptner Hütte", "checkin_from" : ISODate("2017-08-01T00:00:00Z"), "is_delete" : NumberLong(0), "status" : NumberLong(1) }
  { "_id" : ObjectId("598c07b5f2017f945660bc1e"), "cabinname" : "Kemptner Hütte", "checkin_from" : ISODate("2017-08-01T00:00:00Z"), "is_delete" : NumberLong(0), "status" : NumberLong(2) }
  { "_id" : ObjectId("598c07c3f2017f945660bc1f"), "cabinname" : "Kemptner Hütte", "checkin_from" : ISODate("2017-08-01T00:00:00Z"), "is_delete" : NumberLong(0), "status" : NumberLong(1) }
  { "_id" : ObjectId("598c07cdf2017f945660bc20"), "cabinname" : "Kemptner Hütte", "checkin_from" : ISODate("2017-08-01T00:00:00Z"), "is_delete" : NumberLong(0), "status" : NumberLong(1) }
  { "_id" : ObjectId("598c07f5f2017f945660bc21"), "cabinname" : "Kemptner Hütte", "checkin_from" : ISODate("2017-08-02T00:00:00Z"), "is_delete" : NumberLong(0), "status" : NumberLong(2) }
  { "_id" : ObjectId("598c07fef2017f945660bc22"), "cabinname" : "Kemptner Hütte", "checkin_from" : ISODate("2017-08-02T00:00:00Z"), "is_delete" : NumberLong(0), "status" : NumberLong(2) }
  { "_id" : ObjectId("598c0802f2017f945660bc23"), "cabinname" : "Kemptner Hütte", "checkin_from" : ISODate("2017-08-02T00:00:00Z"), "is_delete" : NumberLong(0), "status" : NumberLong(3) }
  { "_id" : ObjectId("598c08a4f2017f945660bc24"), "cabinname" : "Kemptner Hütte", "checkin_from" : ISODate("2017-08-03T00:00:00Z"), "is_delete" : NumberLong(0), "status" : NumberLong(2) }
  { "_id" : ObjectId("598c08acf2017f945660bc25"), "cabinname" : "Kemptner Hütte", "checkin_from" : ISODate("2017-08-03T00:00:00Z"), "is_delete" : NumberLong(0), "status" : NumberLong(1) }
  { "_id" : ObjectId("598c08bcf2017f945660bc26"), "cabinname" : "Kemptner Hütte", "checkin_from" : ISODate("2017-08-04T00:00:00Z"), "is_delete" : NumberLong(0), "status" : NumberLong(3) }
  { "_id" : ObjectId("598c08c3f2017f945660bc27"), "cabinname" : "Kemptner Hütte", "checkin_from" : ISODate("2017-08-04T00:00:00Z"), "is_delete" : NumberLong(0), "status" : NumberLong(2) }
  { "_id" : ObjectId("598c08cef2017f945660bc28"), "cabinname" : "Kemptner Hütte", "checkin_from" : ISODate("2017-08-04T00:00:00Z"), "is_delete" : NumberLong(0), "status" : NumberLong(1) }
  { "_id" : ObjectId("598c08dff2017f945660bc29"), "cabinname" : "Kemptner Hütte", "checkin_from" : ISODate("2017-08-05T00:00:00Z"), "is_delete" : NumberLong(0), "status" : NumberLong(3) }
  { "_id" : ObjectId("598c08eef2017f945660bc2a"), "cabinname" : "Kemptner Hütte", "checkin_from" : ISODate("2017-08-06T00:00:00Z"), "is_delete" : NumberLong(0), "status" : NumberLong(2) }
  { "_id" : ObjectId("598c08f3f2017f945660bc2b"), "cabinname" : "Kemptner Hütte", "checkin_from" : ISODate("2017-08-06T00:00:00Z"), "is_delete" : NumberLong(0), "status" : NumberLong(2) }
  { "_id" : ObjectId("598c0901f2017f945660bc2c"), "cabinname" : "Kemptner Hütte", "checkin_from" : ISODate("2017-08-07T00:00:00Z"), "is_delete" : NumberLong(0), "status" : NumberLong(3) }
  { "_id" : ObjectId("598c0905f2017f945660bc2d"), "cabinname" : "Kemptner Hütte", "checkin_from" : ISODate("2017-08-07T00:00:00Z"), "is_delete" : NumberLong(0), "status" : NumberLong(3) }
  { "_id" : ObjectId("598c090ef2017f945660bc2e"), "cabinname" : "Kemptner Hütte", "checkin_from" : ISODate("2017-08-08T00:00:00Z"), "is_delete" : NumberLong(0), "status" : NumberLong(2) }
  { "_id" : ObjectId("598c091af2017f945660bc2f"), "cabinname" : "Kemptner Hütte", "checkin_from" : ISODate("2017-08-08T00:00:00Z"), "is_delete" : NumberLong(0), "status" : NumberLong(3) }
  { "_id" : ObjectId("598c091ff2017f945660bc30"), "cabinname" : "Kemptner Hütte", "checkin_from" : ISODate("2017-08-08T00:00:00Z"), "is_delete" : NumberLong(0), "status" : NumberLong(1) }
  </code></pre>