我的要求是仅设置BSONDateTime
为特定数据。从我找到了确切的数据,但是在我的输出中java.util.Date
尝试转换时是错误的。BSONDateTime
以下是我的转换代码:
val currentDate: LocalDate = LocalDate.now();
val milliSeconds: Long = currentDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
new BSONDateTime(milliSeconds);
输出:
相位:2015 年 10 月 16 日星期五 00:00:00
实际: 2015 年 10 月 15 日星期五 18:30:00
更新问题
我的基本要求是,我想使用查询在 mongodb 数据中设置和比较我的日期。喜欢:我的 mongodb 文档具有以下结构
{
"_id" : ObjectId("561e62892d0000e98ec782c3"),
"addedOn" : ISODate("2015-10-14T14:11:21.361Z"),
"expiredOn" : ISODate("2015-10-30T00:00:00.000Z")
}
我的第一个查询要求是使用以下代码将过期日期设置为当前日期:
def currentDateOnly: BSONDateTime = {
val currentDate: LocalDate = LocalDate.now();
val milliSeconds: Long = currentDate.atStartOfDay().atZone(ZoneOffset.UTC).toInstant().toEpochMilli();
logger.info("currentDateOnly Conversion: "+currentDate);
new BSONDateTime(milliSeconds);
}
从这段代码中,我找到了上午 12 点的当前日期。我正在将日期转换为milliSeconds
并创建BSONDateTime
对象并使用响应式 mongo 查询设置值,如下所示:
$set(propertyName -> CustomUtility.currentDateOnly) // this is just a set par of update statement.
我的第二个查询要求是使用以下查询匹配“expiredOn”日期:
$doc("expiredOn" $gte CustomUtility.currentDateOnly);
现在,当我在数据库中设置日期时,我的 mongo db 存储无效的日期值,我在上面也提到过:
输出:
相位:2015 年 10 月 16 日星期五 00:00:00
实际: 2015 年 10 月 15 日星期五 18:30:00
我需要存储特定数据并将查询与特定日期进行比较。