Spring引导mongoDB就像数字字段上的查询一样 - 整数/双

时间:2018-01-31 12:59:19

标签: mongodb 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));

return query;

查询:

db.players.find({
  $and: [{
      "type": {
        $in: ["football"]
        }
    },
    {
      "team": {
        $regex: "barcelona",
        $options: "i"
      }
    }]
})

以上查询返回2个“type”文档作为“football”和“team”,如 barcelona

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

提前谢谢。

2 个答案:

答案 0 :(得分:1)

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

return query;

此代码段生成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)"));

    return query;

我希望这会有所帮助。

答案 1 :(得分:0)

基于@Rahul Ghadage的答案,我能够使它工作:

query.addCriteria(Criteria.where("$where").is("/" + salary + "/.test(this.salary)"));

请注意,代码示例中的薪金是您的Java变量,其中填充了您要匹配的数字。 第二个薪金是mongo结构中的字段名称。

请务必在mongo中使用带有用户输入的正则表达式!