Find objects between two dates MongoDB正在响应mongoshell命令。但我需要用Java来做。我对我的问题作了更多的解释。希望这能帮上忙
下面是对象
{
"_id" : ObjectId("5a8f997fcdc2960adae4f91a"),
"cobDate" : ISODate("2018-02-15T18:30:00.000Z"),
"Version" : 1
}在MongoShell中工作的查询
db.getCollection('collection').find({"cobDate" : { "$gt" : ISODate("2018-04-04T00:00:00.000Z"), "$lte" : ISODate("2018-04-06T00:00:00.000Z")}})下面是我的Java代码。
String businessDate = "2018-04-05"; //This is the argument to be passed in string by user
LocalDate sDate = LocalDate.parse(businessDate);
LocalDate eDate = sDate.plusDays(1);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date ssdate = format.parse(sDate.toString());
Date eedate = format.parse(eDate.toString());
String startDate = MongoConnect.toISO8601UTC(ssdate);
String endDate = MongoConnect.toISO8601UTC(eedate);
BasicDBObject query = new BasicDBObject();
query.put("cobDate", BasicDBObjectBuilder.start("$gt", endDate).add("$lte", endDate).get()) ;
db.getCollection(collectionName).find(query)这里的syso(查询)输出是-->
{"cobDate" : { "$gt" : "2018-04-04T00:00:00.000Z", "$lte" : "2018-04-06T00:00:00.000Z" } } 也不适用于我存储像ISODate("2018-04-05T00:00:00.000Z"一样的"cobDate“的地方
发布于 2018-04-19 15:57:15
查找大于或等于日期的日期和小于日期+1天的日期比尝试使用正则表达式解析文本更容易和更快。
String sourceDate = "2018-02-15";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date startDate = format.parse(sourceDate);
Date endDate = DateUtil.addDays(myDate, 1);
collection.find(and(gte("Date", startDate ), lt("Date", endDate )));https://stackoverflow.com/questions/49925152
复制相似问题