[英]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.