0

I need to fetch records from a MongoDB which has a set of documents. I am using Spring Boot and MongoRepository to do the CRUD operations. I am using a Query annotation to pass my Mongo query.But Spring is unable to parse the query that I have written. I am new to MongoDB, so please excuse me if this is a silly question. Any help is appreciated, Thanks.

This is the Query I am using:

@Query("{ '_id.AID' : ?0, '_id.STS' : {'$gte': NumberLong(1475778600)}, '_id.ETS':{'$lte': NumberLong(1475781659)}}")
List<AggregatedData> findByAidAndStartTimeAndEndTime(String aid);

The exception I am getting:

Caused by: com.mongodb.util.JSONParseException: 
{ '_id.AID' : "_param_0", '_id.STS' : {'$gte': NumberLong(1475778600)}, '_id.ETS':{'$lte': NumberLong(1475781659)}}
                                                ^
    at com.mongodb.util.JSONParser.read(JSON.java:301) ~[mongodb-driver-3.2.2.jar:na]
    at com.mongodb.util.JSONParser.parse(JSON.java:180) ~[mongodb-driver-3.2.2.jar:na]

Mongo record structure:

{
    "_id" : {
        "SiteId" : "JATH",
        "AID" : "JA04",
        "STS" : NumberLong(1475778600),
        "ETS" : NumberLong(1475781659)
    },
    "TS" : [ 
        1475820600, 
        1475779200, 
        1475779800, 
        1475780400, 
        1475781000, 
        1475781600
    ],
    "Tags" : {
        "ActivePower" : {
            "Max" : [ 
                1181.5, 
                1181.5, 
                1181.5, 
                1181.5, 
                1181.5, 
                1181.5
            ],
            "Min" : [ 
                73.5, 
                73.5, 
                73.5, 
                73.5, 
                73.5, 
                73.5
            ],
            "Sum" : [ 
                224430, 
                224430, 
                224430, 
                224430, 
                224430, 
                224430
            ],
            "Avg" : [ 
                374.05, 
                374.05, 
                374.05, 
                374.05, 
                374.05, 
                374.05
            ],
            "Count" : [ 
                600, 
                600, 
                600, 
                600, 
                600, 
                600
            ],
            "Std" : [ 
                175.242871942532, 
                175.242871942532, 
                175.242871942532, 
                175.242871942532, 
                175.242871942532, 
                175.242871942532
            ]
        }        
    }
}
Kishor .M
  • 131
  • 1
  • 1
  • 8

2 Answers2

1

Try this:

@Query(value = "{ '_id.AID' : ?0 }", fields = {"_id.STS' : {'$gte': NumberLong(1475778600)}, '_id.ETS':{'$lte': NumberLong(1475781659)}}")
Zildyan
  • 1,261
  • 9
  • 11
1

Answer!!:

Hi, I got it solved.... This is how the query looks:

@Query("{ '_id.AID' : ?0, $and:[{'_id.STS' : {'$gte': ?1}}, {'_id.ETS':{'$lte': ?2}}]}")    
List<AggregatedData> findByAidAndStartTimeAndEndTime(String aid, Long startTimeRange, Long endTimeRange);
Kishor .M
  • 131
  • 1
  • 1
  • 8