MongoRepository JSON 日期查询(春季) [英] MongoRepository JSON Date Query (Spring)
问题描述
我正在尝试使用自己的查询来查询 mongo 存储库:
I am trying to use make my own query for a mongo Repository:
@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);
}
我想要实现的是使用关键字搜索过时的日志.以上抱怨解析错误:
What I'd like to achieve is searching though dated logs with a keyword. The above complains about a parse error:
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 contains UNKNOWN instances
If I replace the ISODate(?0)
with simply ?0
it produces Page 1 of 0 containing UNKNOWN instances
字符串 isoStartDate
&isoEndDate
由 java.util.Date
生成,看起来像这样 2017-06-27T00:00:00.000Z
The Strings isoStartDate
& isoEndDate
are produced from java.util.Date
and look like this 2017-06-27T00:00:00.000Z
我如何在里面找到我的约会对象?
How do I get my date in there?
推荐答案
ISODate 是一个 Mongo shell 构造,用于创建 BSON 日期并且绝对不是有效的 JSON,我相信这是您抱怨的错误.
ISODate is a Mongo shell construct to create a BSON date and definitely not valid JSON, which is what I believe your error is complaining about.
尝试按照中的建议将上述 ISODate 调用替换为 { '$date' : '?0' }
和 { '$date' : '?1' }
这个答案.所有的字符串可能都需要用单引号括起来.
Try replacing the above ISODate calls with { '$date' : '?0' }
and { '$date' : '?1' }
as suggested in this answer. All the strings should probably need to be surrounded in single quotes.
这篇关于MongoRepository JSON 日期查询(春季)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!