Успях да създам заявка LIKE в двойно поле.
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;
Този кодов фрагмент генерира LIKE, както публикувах по-горе.
Също така можете да го направите по лесен начин.
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;
Заявка:
db.players.find({
$and: [{
"type": {
$in: ["football"]
}
},
{
$where : "/^250.*/.test(this.salary)"
}]
})
Надявам се, че това ще помогне.