简体   繁体   English

在Mongodb中找不到Java中确切日期匹配的文档

[英]Unable to find a document in Mongodb where exact date match in java

Statement: I am trying to get the documents from MongoDB collection (Emp) using java. 声明:我正在尝试使用Java从MongoDB集合(Emp)中获取文档。

Condition: Where it matches with the DOB(Date of birth) of a person. 条件:与人的出生日期(DOB)匹配的地方。

Problem: However, it never returns a record. 问题:但是,它从不返回记录。

But it works perfectly for other fields such as EmpID or EmpName etc. The document of my collection looks like this, 但这对其他字段(例如EmpID或EmpName等)非常有效。我收藏的文档看起来像这样,

{ 
    "_id" : ObjectId("5d4d9059f0b31921a4916a0c"), 
    "EmpID" : "1001", 
    "EmpName" : "John", 
    "Sal" : 30000.0, 
    "DOB" : ISODate("1989-06-09T18:30:00.000+0000"), 
    "Age" : 31.0
}

Please find the following java code that I have tried, 请找到我尝试过的以下Java代码,

BasicDBObject dbo = new BasicDBObject();
dbo.append("DOB", new BasicDBObject("$eq","1989-06-10T00:00:00.000"));

FindIterable<Document> doc = coll.find(dbo);

for (Document dox : doc) 
{   
    System.out.println(dox.toJson());
}

Please help 请帮忙

For ISODate it's needed to pass the Date object in BasicDBObject , not String , also timezone must be provided: 对于ISODate,需要在BasicDBObject传递Date对象,而不是String ,还必须提供时区:

dbo.append("DOB", new BasicDBObject("$eq",new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXX").parse("1989-06-10T00:00:00.000+0000")));

For Date Of Birth better to use $gte and $lt comparition operators together in order to take full range of single day, like that: 对于“生日”,最好将$gte$lt比较运算符一起使用,以便完整使用一天,例如:

Date dayStart = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXX").parse("1989-06-10T00:00:00.000+0000");
Date dayEnd = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXX").parse("1989-06-11T00:00:00.000+0000");

BasicDBObject query = new BasicDBObject("Date", new BasicDBObject("$gt", dayStart).append("$lte", dayEnd));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java 中 MongoDB 文档数组的精确文本匹配 - Exact text match on MongoDB document array in Java MongoDB 中的完全匹配文档 - Exact match document in MongoDB Java - MongoDB 不区分大小写,不检查完全匹配 - Java - MongoDB case insensitive not checking for exact match Java-Selenium- 无法找到 CDP 版本 98 的完全匹配,因此返回找到的最接近的版本:97 - Java-Selenium- Unable to find an exact match for CDP version 98, so returning the closest version found: 97 Java - Selenium - 无法找到与 CDP 版本 94 完全匹配的版本,因此返回找到的最接近的版本:93 - Java - Selenium - Unable to find an exact match for CDP version 94, so returning the closest version found: 93 无法找到 CDP 的完全匹配 - 无法从 windows 弹出窗口上传 SELENIUM 和 JAVA 中的文件 - Unable to find an exact match for CDP - Cannot upload a file in SELENIUM & JAVA from windows popup java到mongoDB where子句中的日期 - java to mongoDB Date in where clause Java Mongodb 将正则表达式匹配到任何文档字段 - Java Mongodb match regex to any document field MongoDB Java查找文档并过滤数组字段(删除不符合条件的数组数据) - Mongodb java find document and filter an array field (delete array data that don't match a condition) 如何在文档的子数组中使用mongodb java使用mongoDB查找文档? - how to find document with mongoDB using mongodb java in sub array of document?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM