我正在使用Spring-boot开发一个webApp,我想创建一个查询findByMonth(int或String month),但我希望能够使用例如findByNameAndMonth(String name,month)或findByIdAndMonth()是它可能的?该日期具有LocalDate类型。
我有一个像这样的对象:
public final class Alarm {
@id
private final String id;
private final String type;
// ....
private final LocalDate date;
及其mongoRepository:
public interface AlarmRepository extends MongoRepository<Alarm, String>{
}
该查询应该给出与该查询相同的结果,例如:
db.alarm.find({ $where : 'return this.date.getMonth() == 5'})
LocalDate
是作为ISODate存储在MongoDB中的,因此无法直接访问其属性(date.month
只是不起作用,getMonth()
必须使用方法)。
这导致使用Spring Data中的日期有些棘手。为了解决您的问题,我建议在您的存储库中创建此方法:
List<Alarm> findByDateBetween(LocalDate from, LocalDate to);
但是您必须注意边界条件(一个月的开始和一个月的结束)。
另一种选择是将日期存储为纯对象,并通过其属性存储访问月,如文档所述。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句