@Test
public void saveMany() {
    // 批处理(10万条数据1127)(100万条条数据测试75773)qps13197
    Long startTime = System.currentTimeMillis();
    List<Aqz> aqzList = new ArrayList<>();
    for (int i = 0; i < 1000000; i++) {
        Aqz aqz = new Aqz();
        int random = (int) (Math.random() * 2);
        aqz.setABoolean(random == 0 ? false : true);
        aqz.setCreateDate(new Date());
        aqz.setString("string2" + i);
        aqz.setAnInt(i + 20000);
        aqz.setADouble(Math.random() * 2);
        List list = new ArrayList<>();
        list.add("aaa2" + i);
        list.add("bbb2" + i);
        list.add("ccc2" + i);
        aqz.setList(list);
        aqzList.add(aqz);
        System.out.println(i);
    }
    mongoTemplate.insertAll(aqzList);
    Long endtTime = System.currentTimeMillis();
    Long resultTime = endtTime - startTime;
    System.out.println("[总共执行耗时]" + resultTime);
}
@Test
public void saveClazz() {// 添加课堂
    Clazz clazz = new Clazz();
    clazz.setClazzName("天天课堂3");
    mongoTemplate.insert(clazz);
}

分页,范围,模糊,排序

@Test
public void saveStudent() {// 添加学生
    String clazzName = "天天课堂";
    Query query = new Query(Criteria.where("clazzName").is(clazzName));
    Clazz clazz = mongoTemplate.findOne(query, Clazz.class);
    Student student = new Student();
    student.setStudentName("小红2");
    student.setAge(18);
    student.setClazzId(clazz.getId());
    mongoTemplate.save(student);

}

@Test
public void findOneClazz() {
    Query query = new Query(Criteria.where("_id").is("60e10203d31e2641ecb748ee"));
    Clazz clazz = mongoTemplate.findOne(query, Clazz.class);
    // clazz】】】" + clazz);
}

@Test
public void findByManyStudent1() {// 多条件1
    Query query = new Query();
    query.addCriteria(Criteria.where("studentName").is("小红"));
    query.addCriteria(Criteria.where("age").is(18));
    List<Student> students = mongoTemplate.find(query, Student.class);
    // students);
}

@Test
public void findByManyStudent0() {// 多条件0
    Query query = new Query(Criteria.where("studentName").is("小红"));
    query.addCriteria(Criteria.where("age").is(18));
    List<Student> students = mongoTemplate.find(query, Student.class);
    // students);
}

@Test
public void findByManyStudent3() {// 多条件3
    Query query = new Query(Criteria.where("studentName").is("小红").and("age").is(18));
    List<Student> students = mongoTemplate.find(query, Student.class);
    // students);
}

@Test
public void findByManyStudent2() {// 多条件2
    Student student = new Student();
    student.setStudentName("小红");
    student.setAge(18);
    Query query = new Query(Criteria.byExample(student));
    List<Student> students = mongoTemplate.find(query, Student.class);
    // students);
}

@Test
public void findByManyStudentOr() {// 多条件0 or
    Query query = new Query(Criteria.where("studentName").regex("小"));
    query.addCriteria(new Criteria().orOperator(Criteria.where("age").is(18),
                                                new Criteria().andOperator(Criteria.where("studentName").is("小明"), Criteria.where("age").is(16))));
    List<Student> students = mongoTemplate.find(query, Student.class);
    // students);
}

/*
 * query.addCriteria(
 * new Criteria().orOperator(
 * Criteria.where("value").gte(300),
 * new Criteria().andOperator(
 * Criteria.where("state1").is(11),
 * Criteria.where("state2").is(22)
 * )
 * )
 * );
 */
@Test
public void searchClazzByIn() {// in操作
    List<String> str = new ArrayList<>();
    str.add("天天课堂");
    Query query = new Query(Criteria.where("name").in(str));
    List<Clazz> clazzList = mongoTemplate.find(query, Clazz.class);
    // clazzList】】】" + clazzList);

}

@Test
public void findListStudentSort() {// 排序
    Query query = new Query();
    query.with(Sort.by(Sort.Direction.DESC, "age"));
    List<Student> students = mongoTemplate.find(query, Student.class);
    // students】】】" + students);

}

@Test
public void findFenYeList() {// 分页
    // 设置分页参数
    Query query = new Query();
    int currentPage = 2;// 0,1相同
    int pageSize = 2;
    // 设置分页信息
    query.limit(pageSize);
    query.skip(pageSize * (currentPage - 1));
    // query.addCriteria(Criteria.where("clazzName").regex("天"));
    List<Clazz> clazzes = mongoTemplate.find(query, Clazz.class);
    // clazzs】】】" + clazzes);
}

@Test
public void update() {// 更新
    Query query = Query.query(Criteria.where("id").is("60e103fcd31e2615bcaf91ed"));// 添加查询条件
    // query.addCriteria(Criteria.where("time").gte(beginTime).lte(endTime));
    Update update = new Update();
    update.set("clazzName", "111");
    UpdateResult updateResult = mongoTemplate.updateFirst(query, update, Clazz.class);
    // updateResult.toString());

}

@Test
public void delete() {// 删除
    Query query = Query.query(Criteria.where("id").is("60e103fcd31e2615bcaf91ed"));
    mongoTemplate.remove(query, Clazz.class);
}

@Test
public void findZongHe() {// 分页+范围+模糊查询+排序
    // 拼装查询信息
    Query query = new Query();
    query.addCriteria(Criteria.where("age").gte(6).lte(18));
    query.with(Sort.by(Sort.Direction.ASC, "age"));
    query.addCriteria(Criteria.where("name").regex("小"));
    // 模糊查询名字
    Long count = mongoTemplate.count(query, Student.class);
    // 查询总记录数
    List<Student> list = mongoTemplate.find(query, Student.class);

}

分组,两表三表联查

package com.csw.mongodbspringbootdemo.test;

import com.csw.mongodbspringbootdemo.entity.Chair;
import com.csw.mongodbspringbootdemo.entity.Desk;
import com.csw.mongodbspringbootdemo.entity.Room;
import com.mongodb.BasicDBObject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationOperation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.LookupOperation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@SpringBootTest
@RunWith(SpringRunner.class)
public class RoomDeskRecommend {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Test
    public void saveRoom() {// 添加房间
        Room room = new Room();
        room.setName("空房间2");
        room.setUnitCode(UUID.randomUUID().toString());
        mongoTemplate.save(room);
        Room room2 = new Room();
        room2.setName("空房间1");
        room2.setUnitCode(UUID.randomUUID().toString());
        mongoTemplate.save(room2);

    }

    @Test
    public void saveDesk() {// 添加桌子
        String roomName = "光明房间";
        String deskName = "5号桌子";
        Query query = new Query(Criteria.where("name").is(roomName));
        Room room = mongoTemplate.findOne(query, Room.class);
        Desk desk = new Desk();
        desk.setName(deskName);
        assert room != null;
        desk.setUnitCode(room.getUnitCode());
        mongoTemplate.save(desk);
        System.out.println(room);

        Query query2 = new Query(Criteria.where("name").is(deskName));
        Desk desk2 = mongoTemplate.findOne(query2, Desk.class);
        System.out.println(desk2);

    }

    @Test
    public void groupBy() {// group
        List<AggregationOperation> aggs = new ArrayList<>();
        // aggs.add(Aggregation.match(Criteria.where("name").is("log")));
        aggs.add(Aggregation.group("name").count().as("count"));
        aggs.add(Aggregation.project().and("_id").as("name").and("count").as("count"));

        Aggregation agg = Aggregation.newAggregation(aggs);

        AggregationResults<Map> results = mongoTemplate.aggregate(agg, Desk.class, Map.class);
        for (Map result : results) {
            System.out.println(result);
        }
    }

    @Test
    public void findMoreTable() {// 两表联查

        LookupOperation lookupOperation = LookupOperation.newLookup().from("room"). // 关联表名
            localField("unitCode"). // 主关联字段
            foreignField("unitCode").// 从表关联字段对应的次表字段
            as("rooms");// 查询结果集合名
        Criteria ordercri = Criteria.where("rooms").not().size(0);// 只查询有宠物的人
        // ordercri.and("age").gte(1).lte(5);//只查询1岁到5岁的宠物
        AggregationOperation matchZi = Aggregation.match(ordercri);
        Aggregation aggregation = Aggregation.newAggregation(lookupOperation, matchZi);// 排序
        List<Map> results = mongoTemplate.aggregate(aggregation, "desk", Map.class).getMappedResults(); // 查询出的结果集为BasicDBObject类型
        for (Map result : results) {
            System.out.println(result);
        }
    }

    @Test
    public void findMoreTable2() {// 两表联查

        LookupOperation lookupOperation = LookupOperation.newLookup().from("desk"). // 关联表名
            localField("unitCode"). // 主关联字段
            foreignField("unitCode").// 从表关联字段对应的次表字段
            as("desks");// 查询结果集合名

        Criteria ordercri = Criteria.where("desks").not().size(0);// 只查询有宠物的人
        // ordercri.and("age").gte(1).lte(5);//只查询1岁到5岁的宠物
        AggregationOperation matchZi = Aggregation.match(ordercri);
        Aggregation aggregation = Aggregation.newAggregation(lookupOperation, matchZi);// 排序
        List<Map> results = mongoTemplate.aggregate(aggregation, "room", Map.class).getMappedResults(); // 查询出的结果集为BasicDBObject类型
        for (Map result : results) {
            System.out.println(result);
        }
    }

    @Test
    public void findMoreTableZongHe() {// 两表联查

        int pageNumber = 2;// 0,1相同
        int pageSize = 2;
        // 拼装关联信息
        LookupOperation lookupOperation = LookupOperation.newLookup().from("room"). // 关联表名
            localField("unitCode"). // 主关联字段
            foreignField("unitCode").// 从表关联字段对应的次表字段
            as("ClazzStudents");// 查询结果集合名
        // 拼装具体查询信息
        // 次表
        Criteria ordercri = Criteria.where("ClazzStudents").not().size(0);// 只查询有宠物的人
        // ordercri.and("age").gte(1).lte(5);//只查询1岁到5岁的宠物
        AggregationOperation matchZi = Aggregation.match(ordercri);
        // 主表
        Criteria qqq = Criteria.where("name").regex("号");// 只查询名字中带有文的人
        AggregationOperation matchFu = Aggregation.match(qqq);
        // 分页查询
        Aggregation aggregation = Aggregation.newAggregation(matchFu, lookupOperation, matchZi,
                                                             Aggregation.sort(Sort.Direction.DESC, "name"),
                                                             Aggregation.skip(pageSize > 1 ? (pageNumber - 1) * pageSize : 0), Aggregation.limit(pageSize));// 排序
        // Aggregation.skip(pageable.getPageNumber()>1?(pageable.getPageNumber()-1)*pageable.getPageSize():0),//pagenumber
        // 分页
        /*
     * Aggregation.skip(pageSize>1?(pageNumber-1)*pageSize:0);
     * Aggregation.limit(pageSize);
     */
        // Aggregation.group("name");

        // 总数查询
        Aggregation counts = Aggregation.newAggregation(matchFu, lookupOperation, matchZi);
        int count = mongoTemplate.aggregate(counts, Desk.class, BasicDBObject.class).getMappedResults().size();
        System.out.println("【count】" + count);
        List<Map> results = mongoTemplate.aggregate(aggregation, "desk", Map.class).getMappedResults(); // 查询出的结果集为BasicDBObject类型
        for (Map result : results) {
            System.out.println(result);
        }
    }

    /*
 * public PageResult<T> pagination(Class<T> clazz, int pageSize, int pageNum,
 * Query query) { long total = this.mongoTemplate.count(query, clazz); Integer
 * pages = (int)Math.ceil((double)total / (double)pageSize); if (pageNum <= 0 ||
 * pageNum > pages) { pageNum = 1; } int skip = pageSize * (pageNum - 1);
 * query.skip(skip).limit(pageSize); List<T> list = mongoTemplate.find(query,
 * clazz); PageResult pageResult = new PageResult(); pageResult.setTotal(total);
 * pageResult.setPages(pages); pageResult.setPageSize(pageSize);
 * pageResult.setPageNum(pageNum); pageResult.setList(list); return pageResult;
 * }
 */
    @Test
    public void saveChair() {// 添加椅子
        String roomName = "光明房间";
        String chairName = "1号椅子";
        Query query = new Query(Criteria.where("name").is(roomName));
        Room room = mongoTemplate.findOne(query, Room.class);
        Chair chair = new Chair();
        chair.setName(chairName);
        assert room != null;
        chair.setUnitCode(room.getUnitCode());
        mongoTemplate.save(chair);

    }

    @Test
    public void findMoreTable3_0() {// 三表联查测试,第一个表关联第二个人表(关联字段1),第一个表关联第三个表(关联字段1)

        LookupOperation lookupOperation = LookupOperation.newLookup().from("desk"). // 关联表名
            localField("unitCode"). // 主关联字段
            foreignField("unitCode").// 从表关联字段对应的次表字段
            as("desks");// 查询结果集合名

        Criteria ordercri = Criteria.where("desks").not().size(0);// 只查询有宠物的人
        // ordercri.and("age").gte(1).lte(5);//只查询1岁到5岁的宠物
        AggregationOperation matchZi = Aggregation.match(ordercri);
        LookupOperation lookupOperation2 = LookupOperation.newLookup().from("chair"). // 关联表名
            localField("unitCode"). // 主关联字段
            foreignField("unitCode").// 从表关联字段对应的次表字段
            as("chairs");// 查询结果集合名

        Criteria ordercri2 = Criteria.where("chairs").not().size(0);// 只查询有宠物的人
        // ordercri.and("age").gte(1).lte(5);//只查询1岁到5岁的宠物
        AggregationOperation matchZi2 = Aggregation.match(ordercri2);
        Aggregation aggregation = Aggregation.newAggregation(lookupOperation, matchZi, lookupOperation2, matchZi2);// 排序
        List<Map> results = mongoTemplate.aggregate(aggregation, "room", Map.class).getMappedResults(); // 查询出的结果集为BasicDBObject类型
        for (Map result : results) {
            System.out.println(result);
        }
    }

    // 数据模拟
    @Test
    public void findMoreTable3_1() {// 三表联查测试,第一个表关联第二个人表(关联字段1),第一个表关联第三个表(关联字段2)

        LookupOperation lookupOperation = LookupOperation.newLookup().from("desk"). // 关联表名
            localField("unitCode"). // 主关联字段
            foreignField("unitCode").// 从表关联字段对应的次表字段
            as("desks");// 查询结果集合名

        Criteria ordercri = Criteria.where("desks").not().size(0);// 只查询有宠物的人
        // ordercri.and("age").gte(1).lte(5);//只查询1岁到5岁的宠物
        AggregationOperation matchZi = Aggregation.match(ordercri);
        LookupOperation lookupOperation2 = LookupOperation.newLookup().from("chair"). // 关联表名
            localField("lastCode"). // 主关联字段
            foreignField("lastCode").// 从表关联字段对应的次表字段
            as("chairs");// 查询结果集合名

        Criteria ordercri2 = Criteria.where("chairs").not().size(0);// 只查询有宠物的人
        // ordercri.and("age").gte(1).lte(5);//只查询1岁到5岁的宠物
        AggregationOperation matchZi2 = Aggregation.match(ordercri2);
        Aggregation aggregation = Aggregation.newAggregation(lookupOperation, matchZi, lookupOperation2, matchZi2);// 排序
        List<Map> results = mongoTemplate.aggregate(aggregation, "room", Map.class).getMappedResults(); 
        // 查询出的结果集为BasicDBObject类型
        for (Map result : results) {
            System.out.println(result);
        }
    }

    // 数据模拟
    @Test
    public void findMoreTable3_3() {
        // 三表联查测试,第一个表关联第二个人表(关联字段1),第二个表关联第三个表(关联字段2)
        LookupOperation lookupOperation = LookupOperation.newLookup().from("room"). // 关联表名
            localField("unitCode"). // 主关联字段
            foreignField("unitCode").// 从表关联字段对应的次表字段
            as("rooms");// 查询结果集合名

        Criteria ordercri = Criteria.where("rooms").not().size(0);// 只查询有宠物的人
        // ordercri.and("age").gte(1).lte(5);//只查询1岁到5岁的宠物
        AggregationOperation matchZi = Aggregation.match(ordercri);
        LookupOperation lookupOperation2 = LookupOperation.newLookup().from("chair"). // 关联表名
            localField("rooms.lastCode"). // 主关联字段
            foreignField("lastCode").// 从表关联字段对应的次表字段
            as("chairs");// 查询结果集合名

        Criteria ordercri2 = Criteria.where("chairs").not().size(0);// 只查询有宠物的人
        // ordercri.and("age").gte(1).lte(5);//只查询1岁到5岁的宠物
        AggregationOperation matchZi2 = Aggregation.match(ordercri2);
        Aggregation aggregation = Aggregation.newAggregation(lookupOperation, matchZi, lookupOperation2, matchZi2);// 排序
        List<Map> results = mongoTemplate.aggregate(aggregation, "desk", Map.class).getMappedResults(); // 查询出的结果集为BasicDBObject类型
        for (Map result : results) {
            System.out.println(result);
        }
    }

示例

mongodb 命令实现

{
  "aggregate": "__collection__",
  "pipeline": [
    {
      "$match": {
        "open": 1,
        "closed": 0
      }
    },
    {
      "$lookup": {
        "from": "B",
        "localField": "threadId",
        "foreignField": "threadId",
        "as": "annotation"
      }
    },
    {
      "$unwind": {
        "path": "$annotation",
        "preserveNullAndEmptyArrays": true
      }
    },
    {
      "$match": {
        "$or": [
          {
            "annotation": null
          },
          {
            "annotation.reBacked": 1
          }
        ]
      }
    },
    {
      "$lookup": {
        "from": "C",
        "localField": "threadId",
        "foreignField": "threadId",
        "as": "queue"
      }
    },
    {
      "$unwind": {
        "path": "$queue",
        "preserveNullAndEmptyArrays": true
      }
    },
    {
      "$match": {
        "queue": null
      }
    },
    {
      "$sort": {
        "createTime": -1
      }
    },
    {
      "$skip": 0
    },
    {
      "$limit": 10
    }
  ]
}

其实就是多个$lookup的累加,需要配合$unwind使用。

java springboot 实现

pom.xml文件依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>

接口实现代码如下:

@Override
public List<ThreadEntity> getThreadList(ThreadVo threadVo) throws Exception {
    String source = threadVo.getSource();
    Integer page = threadVo.getPage();
    Integer pageSize = threadVo.getPageSize();
    Integer type = threadVo.getType();

    LookupOperation lookupOperation1 = LookupOperation.newLookup()
        .from("B")
        .localField("threadId")
        .foreignField("threadId")
        .as("annotation");

    LookupOperation lookupOperation2 = LookupOperation.newLookup()
        .from("C")
        .localField("threadId")
        .foreignField("threadId")
        .as("queue");

    UnwindOperation unwind1 = UnwindOperation.newUnwind().path("annotation").noArrayIndex().preserveNullAndEmptyArrays();
    UnwindOperation unwind2 = UnwindOperation.newUnwind().path("queue").noArrayIndex().preserveNullAndEmptyArrays();

    Criteria criteria = Criteria.where("open").is(1).and("closed").is(0);
    AggregationOperation match = Aggregation.match(criteria);

    Criteria criteria1 = new Criteria();
    criteria1.orOperator(
        Criteria.where("annotation").is(null),
        Criteria.where("annotation.reBacked").is(1)
    );
    Criteria criteria2 = new Criteria().where("queue").is(null);
    AggregationOperation match1 = Aggregation.match(criteria1);
    AggregationOperation match2 = Aggregation.match(criteria2);
    AggregationOperation sort = Aggregation.sort(Sort.by(Sort.Order.desc(annotationLabelService.getGetConntentType(type))));
    AggregationOperation skip = Aggregation.skip((page - 1) * pageSize);
    AggregationOperation limit = Aggregation.limit(pageSize);

    Aggregation aggregation = Aggregation.newAggregation(match, lookupOperation1, unwind1, match1, lookupOperation2, unwind2, match2, sort, skip, limit);
    List<ThreadEntity> list = (List<ThreadEntity>) mongoUtils.lookUp(aggregation, "A", ThreadEntity.class);
    return list;
}