我有SQL
SELECT * FROM test WHERE data BETWEEN '2015-08-26' AND '2015-08-26'
MongoDB中的
MongoCollection<Document> coll = db.getCollection("venda");
AggregateIterable<Document> mongoCollectionList = coll.aggregate(
Arrays.asList(
Aggregates.match(Filters.and(Filters.gte("data", "2015-08-26"),
Filters.lte("data", "2015-08-26"))),
Aggregates.project(fields(excludeId())
));
正确的是使用 ISODate
如何将' 2015-08-26 '转换为' 2015-08-26T02:00:00Z '?
韩国社交协会“
答案 0 :(得分:0)
您可以使用Java 8
将ISO字符串日期解析为Instant
,然后将Date
解析为Mongo DB驱动程序。
您不需要汇总用例。
Instant start = LocalDate.parse("2015-08-26").atStartOfDay().atZone(ZoneId.systemDefault()).toInstant();
Instant end = LocalDate.parse("2015-08-26").atStartOfDay().atZone(ZoneId.systemDefault()).toInstant();
MongoCollection<Document> coll = db.getCollection("venda");
FindIterable<Document> mongoCollectionList = coll.find(Filters.and(Filters.gte("data",
Date.from(start)), Filters.lte("data", Date.from(end)))).projection(excludeId());
Java 7更新:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date start = format.parse("2015-08-26");
Date end = format.parse("2015-08-26");