Spring Boot mongoDB喜欢在数字字段上查询-整数/双精度

我想在Spring Boot" title="Spring Boot">Spring Boot时对Integer / Double字段进行LIKE查询。

集合名称:球员

{

"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"

}

而我的spring java代码如下生成查询。

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;

查询:

db.players.find({

$and: [{

"type": {

$in: ["football"]

}

},

{

"team": {

$regex: "barcelona",

$options: "i"

}

}]

})

上面的查询向我返回了2个文档,其中“类型”分别为“足球”和“团队”,如 巴塞罗那

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

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

但我想查询“类型”为“足球”和“工资”像 250 在它

db.players.find({

$and: [{

"type": {

$in: ["football"]

}

},

{

$where : "/^250.*/.test(this.salary)"

}]

})

返回的结果如下。

{"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"}

先感谢您。

回答:

我设法在双字段上建立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;

我希望这将有所帮助。

以上是 Spring Boot mongoDB喜欢在数字字段上查询-整数/双精度 的全部内容, 来源链接: utcz.com/qa/415254.html

回到顶部