我在使用 Java 搜索 mongodb 中的 ISODate 字段时遇到问题。我想找到完全匹配的日期。
以下是我查询第一个集合并获取 ISODate 字段“Timestamp”的方法。一旦我得到这个日期,我想搜索具有相同“时间戳”值的另一个集合。
FindIterable<Document> docList = thermalComfortCollection.find();
for(Document doc: docList) {
String ts = doc.get("Timestamp").toString();
System.out.println(ts);
...
我正在格式化 ISODate,因为它以我想要搜索的不同格式返回日期。因此,我将其转换为这种模式“yyyy-MM-dd HH:mm:ss”
final DateTimeFormatter inputFormat =
DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy");
// The parsed date
final ZonedDateTime parsed = ZonedDateTime.parse(ts, inputFormat);
// The output format
final DateTimeFormatter outputFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String date = outputFormat.format(parsed);
我找不到如何为 ISODate 类型编写精确匹配语句,因此我同时输入 gte 和 lte 条件来获得精确匹配..!
BasicDBObject query = new BasicDBObject("Timestamp", //
new BasicDBObject("$gte", date).append("$lte", date));
System.out.println(query);
并且查询不起作用。你能给我任何评论我的代码的哪一部分是错误的吗?