MongoRepository JSON日期查询(春季) [英] MongoRepository JSON Date Query (Spring)

查看:629
本文介绍了MongoRepository JSON日期查询(春季)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用自己的查询来访问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 containing 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

我如何在那里约会?

推荐答案

ISODate是用于创建BSON日期且绝对无效的JSON的Mongo shell构造,这是我认为您的错误所抱怨的.

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.

按照此答案中的建议,尝试用{ '$date' : '?0' }{ '$date' : '?1' }替换上述ISODate调用.所有的字符串可能都需要用单引号引起来.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆