2017-04-03 49 views
0

我有以下方法春数据蒙戈不绑定某些参数

@Query(value = "{storeId: ?0, code: ?1, $or: [ { campaignId: ?2}, {campaignId: ''}, {campaignId: null}], $or: [ {$and: [ {'effectiveDate.from': { $lte: ?3}}, {'effectiveDate.to': { $gte: ?3}} ]}, {$and: [ {'effectiveDate.from': { $lte: ?4}}, {'effectiveDate.to': { $gte: ?4}} ]}]}") 
List<GiftCard> findByGiftCardExisting(
     String storeId, String code, String campaignId, LocalDateTime from, LocalDateTime to); 

查询已使用蒙戈的控制台测试弹簧蒙戈库。 但是,运行应用程序时,我得到一个JSONParseException。经过一番挖掘,我已经注意到参数$ 2没有像预期那样受到约束。

{storeId: "L_STORE", code: "TESTE1", $or: [ { campaignId: ?2}, {campaignId: ''}, {campaignId: null}], $or: [ {$and: [ {'effectiveDate.from': { $lte: { "$date" : "2017-04-03T03:00:00.000Z"}}}, {'effectiveDate.to': { $gte: { "$date" : "2017-04-03T03:00:00.000Z"}}} ]}, {$and: [ {'effectiveDate.from': { $lte: { "$date" : "2017-04-03T03:00:00.000Z"}}}, {'effectiveDate.to': { $gte: { "$date" : "2017-04-03T03:00:00.000Z"}}} ]}]} 

其余参数按预期工作。有人可以请指出什么是错的?

+0

发生什么情况,如果您简化@Query只使用一个CAMPAIGNID而不是3?我想知道春季的数据是否会让人困惑。 – Gary

回答

1

您的查询有几个问题。

你缺少围绕$or运营商括号,另一种是你有当您使用相同的($or)运算符指定多个表达式中使用明确的$and

https://docs.mongodb.com/manual/reference/operator/query/and/#and-queries-with-multiple-expressions-specifying-the-same-operator

尝试

@Query(value = "{storeId: ?0, code: ?1, $and:[{$or: [ { campaignId: ?2}, {campaignId: ''}, {campaignId: null}]}, {$or: [ {$and: [ {'effectiveDate.from': { $lte: ?3}}, {'effectiveDate.to': { $gte: ?3}} ]}, {$and: [ {'effectiveDate.from': { $lte: ?4}}, {'effectiveDate.to': { $gte: ?4}} ]}]}]}") 
List<GiftCard> findByGiftCardExisting(
    String storeId, String code, String campaignId, LocalDateTime from, LocalDateTime to); 
+0

非常感谢。我永远不会注意到缺少'$和'运算符。 – vbatista

+0

现在已经修复了绑定相同占位符的问题。试试1.10.1及之后的spring mongo版本。更新了答案。这里是信息。 http://stackoverflow.com/questions/43306460/spring-mongorepository-query-jsonparseexception/43311584#43311584。 – Veeram