Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot mongoDB like query on numeric fields-Integer / Double

I want to make LIKE query on Integer / Double field in spring boot.
Collection name : players

{
    "firstName" : "Lionel",
    "lastName" : "messi",
    "team" : "FC Barcelona",
    "salary" : 40000,
    "type" : "football"
},{
    "firstName" : : "Cristiano",
    "lastName" : "Ronaldo",
    "team" : "Real Madrid C.F.",
    "salary" : 35000,
    "type" : "football"
},{
    "firstName" : : "Neymar",
    "lastName" : "Jr",
    "team" : "Paris Saint-Germain F.C.",
    "salary" : 25000,
    "type" : "football"
},{
    "firstName" : "Luis",
    "lastName" : "Alberto",
    "team" : "FC Barcelona",
    "salary" : 25000,
    "type" : "football"
},{
    "firstName" : "Virat",
    "lastName" : "Kohali",
    "team" : "Indian Cricket Team",
    "salary" : 40000,
    "type" : "cricket"
}

And my spring java code as follows which generate query.

String game = "football";
String team = "barcelona";
Double salary = 250;

Query query = new Query();
Set<String> gameType = new HashSet<>();
List<Criteria> andCriteria = new ArrayList<>();

gameType.add(game);

andCriteria.add(Criteria.where("type").in(gameType));
andCriteria.add(Criteria.where("team").regex(team,"i"));

Criteria[] criteriaArray = new Criteria[andCriteria.size()];
criteriaArray = andCriteria.toArray(criteriaArray);
query.addCriteria(new Criteria().andOperator(criteriaArray));

List<Players> players = mongoTemplate.find(query, Players.class);

return players;

Query :

db.players.find({
  $and: [{
      "type": {
        $in: ["football"]
        }
    },
    {
      "team": {
        $regex: "barcelona",
        $options: "i"
      }
    }]
})

Above query returns me 2 documents for "type" as "football" and "team" like barcelona

{"firstName" : "Lionel", "lastName" : "messi", "team" : "FC Barcelona", "salary" : 40000, "type" : "football"},
{"firstName" : "Luis", "lastName" : "Alberto", "team" : "FC Barcelona", "salary" : 25000, "type" : "football"}

But I want query "type" as "football" and "salary" like 250 in it

db.players.find({
  $and: [{
      "type": {
        $in: ["football"]
      }
    },
    {
      $where : "/^250.*/.test(this.salary)"
    }]
})

and the returned result will be as follow.

{"firstName" : : "Neymar", "lastName" : "Jr", "team" : "Paris Saint-Germain F.C.", "salary" : 25000, "type" : "football"},
{"firstName" : "Luis", "lastName" : "Alberto", "team" : "FC Barcelona", "salary" : 25000, "type" : "football"}

Thank you in advance.

like image 904
Rahul Ghadage Avatar asked May 28 '26 07:05

Rahul Ghadage


1 Answers

I managed to build LIKE query on double field.

Query query = new Query();
Set<String> gameType = new HashSet<>();
List<Criteria> andCriteria = new ArrayList<>();

String game = "football";
int salary = 250;   

gameType.add(game);

andCriteria.add(Criteria.where("type").in(gameType));
andCriteria.add(Criteria.where("$where").is("/^" + salary + ".*/.test(this.salary)"));

Criteria[] criteriaArray = new Criteria[andCriteria.size()];
criteriaArray = andCriteria.toArray(criteriaArray);
query.addCriteria(new Criteria().andOperator(criteriaArray));

List<Players> players = mongoTemplate.find(query, Players.class);

return players;

This code snippet generate LIKE as I posted above.
Also you can do it in simple way.

Query query = new Query();
Set<String> gameType = new HashSet<>();
        
String game = "football";
int salary = 250;
        
gameType.add(game);
        
query.addCriteria(Criteria.where("type").in(gameType));
query.addCriteria(Criteria.where("$where").is("/^" + salary + ".*/.test(this.salary)"));

List<Players> players = mongoTemplate.find(query, Players.class);

return players; 

Query:

db.players.find({
  $and: [{
      "type": {
        $in: ["football"]
      }
    },
    {
      $where : "/^250.*/.test(this.salary)"
    }]
})     

I hope this will help.

like image 57
Rahul Ghadage Avatar answered May 31 '26 06:05

Rahul Ghadage



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!