简体   繁体   中英

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.

Special regex metacharacters: ., +, *, ?, ^, $, (, ), [, ], {, }, |, \.

In your situation, promotionInput.getDealType() contains unescaped dollar sign that you should escape before providing a regex search.

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.

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"));
}

Data in MongoDB

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: Searching the value with regex

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) {

query.addCriteria(Criteria.where(PromotionMasterConstants.DEAL_TYPE_DB).regex(promotionInput.getDealType().replace("$", "\$"),"i")); }

It will take Rate $

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM