简体   繁体   English

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

[英]How to use the same parameter for multiple conditions

I'm trying to compare a value with multiples fields with Spring-Data-MongoDB 1.10.0, I'm using @Query because the nested document is an Object with dynamic fields.我正在尝试将具有多个字段的值与 Spring-Data-MongoDB 1.10.0 进行比较,我正在使用 @Query,因为嵌套文档是一个带有动态字段的 Object。

But if I use the same identifier like?5 in all the conditions, I got an error:但是如果我在所有条件下都使用相同的标识符 like?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} ] }

I'm trying like this:我正在尝试这样:

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

I've tried with only?我试过只有? and [5].和[5]。

Is it possible and could anyone explain how to do this?有可能吗,谁能解释一下如何做到这一点?

I found the solution after debugging the binding process.我在调试绑定过程后找到了解决方案。

I just need to add single quotes in the parameter that I need to repeat, like this:我只需要在需要重复的参数中添加单引号,如下所示:

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

So the parameter number five could be use more than once.因此,参数 5 可以多次使用。

As suggested by Guilherme, that solution can only work for string parameters as using '?0' will convert parameter of any type to string and will give incorrect results.正如 Guilherme 所建议的那样,该解决方案只能用于字符串参数,因为使用 '?0' 会将任何类型的参数转换为字符串,并且会给出不正确的结果。 Passing an extra parameter in the method will same value did the work for me.在方法中传递一个额外的参数将为我完成相同的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在if else循环中对同一数据使用多个条件? -Java / Android - How to use multiple conditions on the same data in an if else loop? - java/android 在 SQL 查询的 WHERE 条件中多次使用相同的参数以用于 JDBC - Utilise the same parameter multiple times in WHERE conditions of an SQL query for use in JDBC 如何使用具有相同参数类型的多个构造函数创建类 - How to create a Class with multiple constructors that use the same parameter type 如何针对多个和/或条件使用Criteria API对象 - How to use Criteria API objects for multiple and/or conditions 如何使用 ORDER BY 按多个排序条件排序 - How to use ORDER BY to sort by multiple sorting conditions 如何使用正则表达式验证 JAVA 中的多个条件? - How to use regex for verifying multiple conditions in JAVA? 如何使用多个ArgumentCaptor参数 - How to use multiple ArgumentCaptor parameter 如何使用通过 kotlin 传递的相同参数为 IN 子句使用多个值 - How to use multiple values for IN clause using same parameter passed through kotlin 如何使用相同的参数但不同的where条件查询同一张表并最终获得组合结果 - How to query same table with same parameter but with different where conditions and get combined result at last 如何在super关键字内使用具有多个条件的三元运算符? - How to use a ternary operator with multiple conditions inside of the super keyword?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM