0

I want to store trace info like below:

{
    "timestamp": 1394343677415,
    "info": {
        "method": "GET",
        "path": "/trace",
        "headers": {
            "request": {
                "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
                "Connection": "keep-alive",
                "Accept-Encoding": "gzip, deflate",
                "User-Agent": "Mozilla/5.0 Gecko/Firefox",
                "Accept-Language": "en-US,en;q=0.5",
                "Cookie": "_ga=GA1.1.827067509.1390890128; ..."
                "Authorization": "Basic ...",
                "Host": "localhost:8080"
            },
            "response": {
                "Strict-Transport-Security": "max-age=31536000 ; includeSubDomains",
                "X-Application-Context": "application:8080",
                "Content-Type": "application/json;charset=UTF-8",
                "status": "200"
            }
        }
    }

My @Document entity is extending HashMap.

Now I have to write custom query for pagination.

In Mongo client shell I would write it:

db.traceInfo.find({"headers.response.status": "404"}).limit(n);

and it works, but I don't know how to write this query as @Query in Spring MongoRepository? How can I do that?

user
  • 4,410
  • 16
  • 57
  • 83

1 Answers1

1

This is totally easy, but keyword limit is not supported by spring-data-mongodb, check out the reference here: https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#repository-query-keywords

Possible solution:

@Query({"headers.response.status": "?1"})
List<T> findByGivenStatus(int status);
Jaiwo99
  • 9,687
  • 3
  • 36
  • 53
  • Thanks, so how can I get Page where "headers.response.status": "500"? Would it work: @Query({"headers.response.status": "?2"}) Page findByGivenStatus(Pageable pageable, int status); – user Oct 25 '17 at 22:29
  • @user just test it, it would work – Jaiwo99 Oct 27 '17 at 07:22