gpt4 book ai didi

java - Spring data mongo未绑定(bind)某些参数

转载 作者:行者123 更新时间:2023-12-02 13:30:42 26 4
gpt4 key购买 nike

我有一个带有以下方法的 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 没有按预期绑定(bind)。

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

其余参数按预期工作。有人可以指出哪里出了问题吗?

最佳答案

您的查询有几个问题。

您缺少 $or 运算符周围的括号,另一个是当您使用相同的指定多个表达式时,您必须使用显式 $and ($or) 运算符。

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);

关于java - Spring data mongo未绑定(bind)某些参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43196157/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com