问题
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