问题
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
回答1:
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
来源:https://stackoverflow.com/questions/29015849/group-by-date-in-mogodb-query-without-considering-time