spring data mongodb -插入一个日期加上一些天使用mongodb服务器时间



在Oracle中,我们可以在插入日期时使用sysdate并对其进行算术运算,从而不依赖于java应用程序服务器的时间。

我知道我们可以通过update使用$currentDate更新修饰符。currentDate在春季数据,但我如何添加X天,例如?

这个问题是关于MongoDB而不是Oracle的。: -)

您可以通过INTERVAL语句,但是当您用小时转换天数时,它可以正常工作。

请像这样构建查询。

@Query(value = "select * from activation_codes a where a.type = :type and a.id_account = :id_account and a.creation_time <= NOW() - :hoursAgo * INTERVAL '1 hour'", nativeQuery = true)
List<ActivationCode> getAllAddedAtLeastXHoursAgo(@Param("id_account") int id_account, @Param("type") int type, @Param("hoursAgo") int hoursAgo);

这将帮助您比较DateTime与x天(将天转换为小时)。