我有一个spring mongo存储库,其中包含以下方法
@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);
使用mongo的控制台测试了查询。 但是,在运行应用程序时,我得到一个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 :(得分:1)
您的查询存在以下几个问题。
您缺少$or
运算符周围的括号,另一个是在使用带有相同($and
)运算符的指定多个表达式时必须使用显式$or
。
尝试
@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);