MongoRepository JSON 日期查询 (Spring)
MongoRepository JSON Date Query (Spring)
我正在尝试对 mongo 存储库使用自己的查询:
@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"}} ]}
^
如果我用简单的 ?0
替换 ISODate(?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 调用。所有字符串可能都需要用单引号括起来。
我正在尝试对 mongo 存储库使用自己的查询:
@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"}} ]}
^
如果我用简单的 ?0
替换 ISODate(?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 调用。所有字符串可能都需要用单引号括起来。