Spring Data MongoDB 教程

Criteria 逻辑查询

MongoDB 逻辑操作符用于将多个条件组合成一个复合条件,常见的逻辑操作符有 $and(与)、$or(或)和 $not(非)。逻辑操作符详细说明如下:

  • $and 逻辑和(与)操作符,需要同时满足具有两个或多个表达式的数组中的条件。例如:

db.t_01.find( {  $and : [ {"age":{ $gte : 28 } }, { "deparment" : { $eq : "sale_01"} } ]  } )

上面将查询 deparment 等于 “sale_01” 且 age 大于等于 28 的文档。

  • $not 逻辑否操作符,返回与查询表达式不匹配的文档。例如:

db.t_01.find( { "age"  : { $not : { $gt : 30 } } } )

上面将查询 age 不大于 30 的文档。

  • $nor 逻辑 NOR 操作符,返回同时不能匹配数组中表达式的文档。例如:

db.t_01.find( { $nor:  [ {"age": 30 } , { "name": "david" } ] } )

上面将查询 age 不等于 30,且 name 不等于 “david” 的文档。

  • $or 逻辑或操作符,返回符合任一条件的所有文档。例如:

db.t_01.find( {  $or : [ { "deparment": "sale_01" }, { "age":{ $gt : 28 } } ] } )

上面将查询 department 等于 “sale_01” 或者 age 大于 28 的文档。

下面将介绍怎样使用 Criteria 实现逻辑查询,Criteria 类定义的逻辑查询方法如下:

  • Criteria andOperator(Collection<Criteria> criteria) 使用 $and 操作符创建一个 Criteria

  • Criteria andOperator(Criteria... criteria)

  • Criteria orOperator(Collection<Criteria> criteria)  使用 $or 操作符创建一个 Criteria

  • Criteria orOperator(Criteria... criteria)

  • Criteria not() 使用 $not 操作符创建一个 Criteria

  • Criteria norOperator(Collection<Criteria> criteria) 使用 $nor 操作符创建一个 Criteria

  • Criteria norOperator(Criteria... criteria)

示例

下面示例将演示怎样使用 Criteria 的逻辑查询方法实现逻辑查询,示例如下:

package com.hxstrive.springdata.mongodb;

import com.hxstrive.springdata.mongodb.entity.Person;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@SpringBootTest
class CriteriaDemo2 {

   @Autowired
   private MongoTemplate mongoTemplate;

   @BeforeEach
   public void init() {
       // 删除集合
       mongoTemplate.dropCollection(Person.class);

       // 准备数据
       mongoTemplate.insert(Person.builder().id(100).name("Tom").age(27).email("Tom@sina.com").build());
       mongoTemplate.insert(Person.builder().id(200).name("Helen").age(30).email("Helen@outlook.com").build());
       mongoTemplate.insert(Person.builder().id(300).name("Bill").age(47).email("bill@gmail.com").build());
       mongoTemplate.insert(Person.builder().id(400).name("Joe").age(20).email("joe@163.com").build());
   }

   @Test
   public void andOperator() {
       // 查询 name=Tom 并且 age>20 的文档
       Criteria criteria = new Criteria();
       criteria.andOperator(
               Criteria.where("name").is("Tom"),
               Criteria.where("age").gt(20)
       );

       List<Person> personList = mongoTemplate.query(Person.class)
               .matching(Query.query(criteria)).all();
       for(Person person : personList) {
           System.out.println(person);
       }
       // 结果:
       // Person(id=100, name=Tom, age=27, email=Tom@sina.com)
   }


   @Test
   public void orOperator() {
       // 查询 name=Tom 或者 age>20 的文档
       Criteria criteria = new Criteria();
       criteria.orOperator(
               Criteria.where("name").is("Tom"),
               Criteria.where("age").gt(20)
       );

       List<Person> personList = mongoTemplate.query(Person.class)
               .matching(Query.query(criteria)).all();
       for(Person person : personList) {
           System.out.println(person);
       }
       // 结果:
       // Person(id=100, name=Tom, age=27, email=Tom@sina.com)
       // Person(id=200, name=Helen, age=30, email=Helen@outlook.com)
       // Person(id=300, name=Bill, age=47, email=bill@gmail.com)
   }


   @Test
   public void norOperator() {
       // 查询 name 不等于 Tom 且 age 不大于 30 的文档
       Criteria criteria = new Criteria().norOperator(
               Criteria.where("name").is("Tom"),
               Criteria.where("age").gt(30)
       );

       // 分析:          等于Tom   大于30
       // Tom      27     Y        N
       // Helen    30     N        N           √
       // Bill     47     N        Y
       // Joe      20     N        N           √

       List<Person> personList = mongoTemplate.query(Person.class)
               .matching(Query.query(criteria)).all();
       for(Person person : personList) {
           System.out.println(person);
       }
       // 结果:
       // Person(id=200, name=Helen, age=30, email=Helen@outlook.com)
       // Person(id=400, name=Joe, age=20, email=joe@163.com)
   }


   @Test
   public void not() {
       // 查询 age 不大于 30 的文档
       Criteria criteria = Criteria.where("age").not().gt(30);

       List<Person> personList = mongoTemplate.query(Person.class)
               .matching(Query.query(criteria)).all();
       for(Person person : personList) {
           System.out.println(person);
       }
       // 结果:
       // Person(id=100, name=Tom, age=27, email=Tom@sina.com)
       // Person(id=200, name=Helen, age=30, email=Helen@outlook.com)
       // Person(id=400, name=Joe, age=20, email=joe@163.com)
   }

}
说说我的看法
全部评论(
没有评论
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号