0

I have a collection of data stored in mongodb, but it stores them with a date time format, but I want to count all records that occur on each day. The sql I am trying to reproduce is:

select count(*)
,      mydate
from   mytable
group by mydate

Currently, if i do

o1 := bson.M{
  $match : bson.M {
     "name" : result.Id,
  },
}

o2 := bson.M {
  "$project" : bson.M {
    "_id" : 1,
    "mydate" : 1,
  },
}

o3 := bson.M {
  "$group" : bson.M {
    "_id" : "$mydate",
    "total" : bson.M { "$sum" : 1 },
  },
}

operators := []bson.M{o1, o2, o3)
pipe := dbObject.Pipe(operators)
results := []bson.M{]
err := pipe.All(&results)

This returns 1 single record for each date/time row. I would like to strip the time and get all records by the date only.

  • And please don't state *"But I'm using mgo!"*, since regardless of the language implementation the answer is to use the [Date Aggregation Operators](https://docs.mongodb.org/manual/reference/operator/aggregation-date/) to just get the required "day" component or use Date Math to round the date to just the day component. – Blakes Seven Feb 26 '16 at 00:50
  • Thanks Blake. I was just trying to provide as much detail as possible. It does look like this is a duplicate. My search didn't turn that one up. – user3295797 Feb 26 '16 at 00:52
  • Actually it's right on the top of results with google ["mongodb group by date"](https://www.google.com/?q=mongodb+group+by+date), with what would seem to be the logical search. Don't forget to confirm the duplicate like the big box on your question is asking you to do. – Blakes Seven Feb 26 '16 at 01:04

0 Answers0