Saya berhasil membuat kueri LIKE di bidang ganda.
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;
Cuplikan kode ini menghasilkan LIKE seperti yang saya posting di atas.
Anda juga dapat melakukannya dengan cara yang sederhana.
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;
Pertanyaan:
db.players.find({
$and: [{
"type": {
$in: ["football"]
}
},
{
$where : "/^250.*/.test(this.salary)"
}]
})
Saya harap ini akan membantu.