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.