0

我需要从具有一组文档的 MongoDB 中获取记录。我正在使用 Spring Boot 和 MongoRepository 来执行 CRUD 操作。我正在使用查询注释来传递我的 Mongo 查询。但是 Spring 无法解析我编写的查询。我是 MongoDB 新手,所以如果这是一个愚蠢的问题,请原谅我。任何帮助表示赞赏,谢谢。

这是我正在使用的查询:

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

我得到的例外:

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记录结构:

{
    "_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
            ]
        }        
    }
}
4

2 回答 2

1

尝试这个:

@Query(value = "{ '_id.AID' : ?0 }", fields = {"_id.STS' : {'$gte': NumberLong(1475778600)}, '_id.ETS':{'$lte': NumberLong(1475781659)}}")
于 2016-10-18T19:04:47.270 回答
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);
于 2016-10-25T07:38:42.000 回答