按日期在mogodb查询中分组而不考虑时间 [英] group by date in mogodb query without considering time
问题描述
我有以下代码:
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);
按日期分组并显示可以正常工作的计数,但问题是我的日期采用以下格式:
日期":ISODate("2014-11-19T04:11:22.000Z")
您也可以看到它也有时间,但是我想按日期分组而不考虑时间,所以我想这样考虑我的日期:
2014-11-19T04:00:00.000Z但我不知道该怎么做...在mysql中是否有像日期一样的mongodb函数可以做到这一点?
任何人都可以帮忙吗?
更新:
我认为这可能会有所帮助,但仍然不知道该怎么做: 可能有用的链接
该解决方案取决于您是否需要将结果"Date"作为ISODate()类型,或者是否可以将其作为字符串取回. >
如果您对字符串没问题,请引入新版本的MongoDB(3.0) $ dateToString 投影运算符.
如果必须是ISODate(),则可以使用我在此博客文章中详细描述的技巧: 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: Can anyone help? Update: I think this might be helpful but still do not know how to do it:
link that might help 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 这篇关于按日期在mogodb查询中分组而不考虑时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
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?