MongoRepository JSON日期查询(春季)
问题描述:
我试图用让我自己查询的蒙戈库:MongoRepository JSON日期查询(春季)
@Repository
public interface LogEntryRepository extends MongoRepository<LogEntry,String> {
@Query("{'created_at' : {{ $gte: ISODate(?0)},{$lt: ISODate(?1)}}, " +
"$or: [{'site': {$regex: ?2}}, {'login': {$regex: ?2}}, {'ip': {$regex: ?2}} ]" +
"}")
public Page<LogEntry> findByDateTimeBetweenAndCriteria(String isoStartDate, String isoEndDate, String searchTerm, Pageable page);
}
我想实现的是,虽然搜索与关键字过时的日志。上述抱怨解析错误:
Caused by: com.mongodb.util.JSONParseException:
{'created_at' : { $gte: ISODate("_param_0"), $lt: ISODate("_param_1")}, $or: [{'site': {$regex: "_param_2"}}, {'login': {$regex: "_param_2"}}, {'ip': {$regex: "_param_2"}} ]}
^
如果我更换ISODate(?0)
只是?0
它产生Page 1 of 0 containing UNKNOWN instances
颖isoStartDate
& isoEndDate
从java.util.Date
生产,像这样2017-06-27T00:00:00.000Z
怎么办我在那里得到我的约会?
答
ISODate是一个Mongo shell构造,用于创建一个BSON日期,并且绝对不是有效的JSON,这是我相信你的错误正在抱怨的东西。
尝试使用{ '$date' : '?0' }
和{ '$date' : '?1' }
代替上述ISODate调用,如answer中所建议。所有的字符串可能都需要用单引号括起来。
即使这会导致JSON解析错误:@Query(“{'created_at':{$ date:?0}}”)'与?0 = 2017-06-28T00:00:00.000Z(字符串)。有什么想法吗? – OrangePot
@OrangePot你应该在'“$ date”周围加双引号' – Kdawg
这个似乎没有什么区别,我把escape放在'$ date'附近,这个小符号'^'在错误之下时,消息显示 – OrangePot