2

I have the following code:

DBObject groupFields = new BasicDBObject("_id", "$Date");
    groupFields.put("count", new BasicDBObject("$sum", 1));
    DBObject groupBy = new BasicDBObject("$group", groupFields);
    stages.add(groupBy);
    DBObject project = new BasicDBObject("_id", 0);
    project.put("count", 1);
    project.put("Date", "$_id");
    stages.add(new BasicDBObject("$project", project));

    DBObject sort = new BasicDBObject("$sort", new BasicDBObject("Date", 1));
    stages.add(sort);

to group by Date and show the count which works fine but the problem is my date is in the following format:

"Date" : ISODate("2014-11-19T04:11:22.000Z")

and as you can see it has time as well but I want to group by date without considering time so I want to consider my date like this:
2014-11-19T04:00:00.000Z but I do not know how to do that ...is there any mongodb function like date in mysql to do that?

Can anyone help?

Update:

I think this might be helpful but still do not know how to do it: link that might help

HMdeveloper
  • 2,772
  • 9
  • 45
  • 74
  • I describe a detailed way to do exactly this here: http://www.kamsky.org/stupid-tricks-with-mongodb/stupid-date-tricks-with-aggregation-framework but in 3.0.0 you can use $dateToString if you don't mind having a string be your _id/Date. – Asya Kamsky Mar 16 '15 at 20:36

1 Answers1

3

The solution depends on whether you need to have the result "Date" as ISODate() type or if it's okay to get it back as a string.

If you are okay with string, new version of MongoDB (3.0) introduced $dateToString projection operator.

If it has to be ISODate() you can use the trick I describe in details in this blog post: http://www.kamsky.org/stupid-tricks-with-mongodb/stupid-date-tricks-with-aggregation-framework

Asya Kamsky
  • 41,784
  • 5
  • 109
  • 133
  • Thanks a lot , I think $dateToString works for me but I do not know how to apply $dateToString: { format: "%Y-%m-%d", date: "$date" } to grouping by line in java : DBObject groupFields = new BasicDBObject("_id", "$Date"); Do you have any idea? – HMdeveloper Mar 16 '15 at 21:02
  • @HamedMinaee, before the `$group` stage, just add a `$project` stage and extract the date alone using `$dateToString`. In the `$group` stage, you can use the extracted date for grouping by. – Anand Jayabalan Mar 17 '15 at 02:38
  • @AnandJayabalan thank u :) . My problem is I can not put format and date all together in projection I have this but not working :DBObject project1 = new BasicDBObject("$dateToString", "format: \"%Y-%m-%d\", date: \"$date"); DBObject groupFields = new BasicDBObject("_id", project1); groupFields.put("count", new BasicDBObject("$sum", 1)); DBObject groupBy = new BasicDBObject("$group", groupFields); – HMdeveloper Mar 17 '15 at 02:53
  • do you mean "can not put .." as in "I'm trying and can't make it work"? The issue is how you are constructing the object - you might try writing it in json and then converting it. here's an example: http://www.mkyong.com/mongodb/java-mongodb-convert-json-data-to-dbobject/ – Asya Kamsky Mar 17 '15 at 18:37
  • @AnandJayabalan I mean I can not figure out how to do the following :DBObject project1 = new BasicDBObject("$dateToString", "format: \"%Y-%m-%d\", date: \"$date"); even I tried it this way but I got error: DBObject format = new BasicDBObject("format", "%Y-%m-%d"); format.put("date", "$Date"); DBObject formattedDate = new BasicDBObject("$dateToString", format); project.put("yearMonthDay", formattedDate); – HMdeveloper Mar 18 '15 at 03:05