Skip to content

Instantly share code, notes, and snippets.

@keepcosmos
Created April 10, 2014 05:06
Show Gist options
  • Select an option

  • Save keepcosmos/10344110 to your computer and use it in GitHub Desktop.

Select an option

Save keepcosmos/10344110 to your computer and use it in GitHub Desktop.

Revisions

  1. keepcosmos created this gist Apr 10, 2014.
    14 changes: 14 additions & 0 deletions mongodb_groupby.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    db.messenger_rooms.group({
    keyf: function(doc){
    var date = new Date(doc.created_at);
    date.setHours(date.getHours() + 9);
    var dateKey = (date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate());
    return {'day':dateKey};
    },
    cond: {created_at: {$gte: ISODate("2014-04-01"), $lte: ISODate("2014-04-03")}},
    initial: { count: 0, reserved_count: 0},
    reduce: function(doc, aggregator){
    aggregator.count += 1;
    if(doc.reservation_status !== undefined && doc.reservation_status !== null) aggregator.reserved_count++;
    }
    });