[英]Spring boot add criteria query
When Deal Type is Rate $ it is not searching/getting but when deal type is Rate % it is working.当交易类型为费率 $ 时,它不会搜索/获取,但当交易类型为费率 % 时,它正在工作。
if(promotionInput.getDealType() != null) {
query.addCriteria(Criteria.where(PromotionMasterConstants.DEAL_TYPE_DB).regex(promotionInput.getDealType(),"i"));
}
I am not getting the error.我没有收到错误。
You are providing regex pattern ( promotionInput.getDealType()
in this case) without escaping special regex characters.您正在提供没有 escaping 特殊正则表达式字符的正则表达式模式(在这种情况下为promotionInput.getDealType()
)。
Special regex metacharacters: ., +, *, ?, ^, $, (, ), [, ], {, }, |, \.
特殊的正则表达式元字符: ., +, *, ?, ^, $, (, ), [, ], {, }, |, \.
In your situation, promotionInput.getDealType()
contains unescaped dollar sign that you should escape before providing a regex search.在您的情况下, promotionInput.getDealType()
包含未转义的美元符号,您应该在提供正则表达式搜索之前对其进行转义。
If a dollar sign ($) is at the end of the entire regular expression, it matches the end of a line.如果美元符号 ($) 位于整个正则表达式的末尾,则它匹配行尾。
If an entire regular expression is enclosed by a caret and dollar sign (^like this$), it matches an entire line.如果整个正则表达式由插入符号和美元符号(^like this$)括起来,它匹配整行。
if(promotionInput.getDealType() != null) {
// maybe check for other metacharacters?
String escapedDealType = promotionInput.getDealType().replace("$", "\\$");
query.addCriteria(Criteria.where(PromotionMasterConstants.DEAL_TYPE_DB)
.regex(escapedDealType,"i"));
}
rs0:PRIMARY> db.promotion.find()
{ "_id" : ObjectId("6034bbfee56ebe09e8821e44"), "value" : "Rate $", "_class" : "com.xyz.model.Promotion" }
{ "_id" : ObjectId("6034bbfee56ebe09e8821e45"), "value" : "Rate %", "_class" : "com.xyz.model.Promotion" }
Test is executed successfully and green.测试已成功执行并呈绿色。
@Test
public void name() {
// drop everything
mongoTemplate.dropCollection(Promotion.class);
// persist entity with value field equls to "Rate $"
final Promotion promotion = new Promotion();
promotion.setValue("Rate $");
mongoTemplate.insert(promotion);
// persist entity with value field equls to "Rate %"
final Promotion promotion2 = new Promotion();
promotion2.setValue("Rate %");
mongoTemplate.insert(promotion2);
// RETURNS EMPTY COLLECTION
Query query = new Query();
query.addCriteria(Criteria.where("value").regex("Rate $", "i"));
List<Promotion> promotions = mongoTemplate.find(query, Promotion.class);
assertThat(promotions.size(), is(0));
// query with escaped $
// FINDS THE RECORD SUCCESSFULLY
Query query2 = new Query();
query2.addCriteria(Criteria.where("value").regex("Rate \\$", "i"));
promotions = mongoTemplate.find(query2, Promotion.class);
assertThat(promotions.size(), is(1));
assertThat(promotions.get(0).getValue(), is("Rate $"));
}
if(promotionInput.getDealType() != null) { if(promotionInput.getDealType() != null) {
query.addCriteria(Criteria.where(PromotionMasterConstants.DEAL_TYPE_DB).regex(promotionInput.getDealType().replace("$", "\$"),"i")); query.addCriteria(Criteria.where(PromotionMasterConstants.DEAL_TYPE_DB).regex(promotionInput.getDealType().replace("$", "\$"),"i")); } }
It will take Rate $这将需要费率 $
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.