java - 如何在多个条件下使用相同的参数

标签 java spring-data-mongodb

我正在尝试使用 Spring-Data-MongoDB 1.10.0 比较具有多个字段的值,我使用 @Query 因为嵌套文档是具有动态字段的对象。

但是如果我在所有条件下使用相同的标识符(如 ?5),则会出现错误:

exception="com.mongodb.util.JSONParseException: 
{ companyType: "SPONSOR", companyId: 6710890, delivered: false, createdAt: { $gt: { "$date" : "2019-09-01T03:00:00.000Z"}, $lt: { "$date" : "2019-09-26T02:59:59.999Z"} }, $or:[ {requestPayload.sponsorGovernmentId: "73068519000185"}, {requestPayload.buyerGovernmentId: "73068519000185"5} ] }

我正在尝试这样:

@Query(value = "{ companyType: ?0, companyId: ?1, delivered: ?2, " +
            "createdAt: { $gt: ?3, $lt: ?4 }, $or:[ {requestPayload.sponsorGovernmentId: ?5}, {requestPayload.buyerGovernmentId: ?5} ] }")
    Page<WebHookDelivery> findByCompanyTypeAndCompanyIdAndDeliveredAndCreatedAtIsBetweenAndAnyKey(String companyType,
                                                                                                  Integer companyId,
                                                                                                  Boolean delivered,
                                                                                                  Date createdAtStart,
                                                                                                  Date createdAtEnd,
                                                                                                  String governmentId,                  
                                                                                                  Pageable pageable);

我只尝试过?和[5]。

这可能吗?有人能解释一下如何做到这一点吗?

最佳答案

调试绑定(bind)过程后找到了解决方案。

我只需要在需要重复的参数中添加单引号,如下所示:

"$or: [{ $or: [{ $or: [{ $or: [{ requestPayload.sponsorGovernmentId: ?5 }, " +
            "{requestPayload.buyerGovernmentId: '?5'}] }, {requestPayload.supplierGovernmentId: '?5'}] }, " +
            "{requestPayload.invoiceNumber: ?6}]}, {requestPayload.externalId: ?7}]

因此参数 5 可以多次使用。

关于java - 如何在多个条件下使用相同的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58174921/

相关文章:

java - (Java新手)实例化未知数量的对象

java - Yarn 容器上的 ExitCodeException

java - 是否有适用于 MongoDB 的 DbUnit 替代方案?

java - Spring MongoDB 数据无法使用 "find"查询获取 @DBRef 对象

java - HtmlUnit 安全警告 : Please treat the URL above as you would your password and do not share it with anyone

java - Java 8 ifPresent与三元运算符

javascript - 当应用程序最小化/关闭时更新 Firestore 值

geojson - 没有找到适合 GeoJsonPoint 类型的构造函数

mongodb - 按匹配的子文档排序

java - Spring Cloud Stream与RabbitMQ Binder,如何应用@Transactional?