Ik ben erin geslaagd om LIKE-query op dubbel veld te bouwen.
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;
Dit codefragment genereert LIKE zoals ik hierboven heb gepost.
Je kunt het ook op een eenvoudige manier doen.
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;
Vraag:
db.players.find({
$and: [{
"type": {
$in: ["football"]
}
},
{
$where : "/^250.*/.test(this.salary)"
}]
})
Ik hoop dat dit zal helpen.