How to use the same parameter for multiple conditions

百般思念 提交于 2020-01-05 06:37:39

问题


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.

But if I use the same identifier like ?5 in all the conditions, I got an error:

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

Is it possible and could anyone explain how to do this?


回答1:


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.



来源:https://stackoverflow.com/questions/58174921/how-to-use-the-same-parameter-for-multiple-conditions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!