mongoTemplate --API

主要讲解MongoTemplate 相关API, 使用springboot + mongoDB环境

1.1 保存,插入文档数据 

  下图中指定保存当前文档的集合为col, 如果不指定默认即为当前Model对象

@RestController
public class ShopController {

    @Autowired
    MongoTemplate mongoTemplate;

    @GetMapping("/save")
    public void save() {
        ShopDO shop1 = new ShopDO(100L,"菜鸟教程");
        ShopDO shop2 = new ShopDO(101L,"有道笔记");
        mongoTemplate.save(shop1,"col");
        mongoTemplate.save(shop2,"col");
        System.out.println("mongoDB插入数据成功,集合为col,文档为:"+mongoTemplate.getCollection("col"));
        List list = mongoTemplate.findAll(ShopDO.class,"col");
        System.out.println("mongoDB查询数据成功,集合为col,文档为:");
        for (ShopDO shopDO:list){
            System.out.println(shopDO.getId()+"/"+shopDO.getName());
        }
    }
mongoDB插入数据成功,集合为col,文档为:DBCollection{database=DB{name='springboot-db'}, name='col'}
mongoDB查询数据成功,集合为col,文档为:
100/菜鸟教程
101/有道笔记

1.2 单条件查询

Query query = Query.query(Criteria.where("id").is(100L));
List list2 = mongoTemplate.find(query,ShopDO.class,"col");
System.out.println("mongoDB按条件ID查询数据成功,集合为col,文档为:");
for (ShopDO shopDO:list2){
  System.out.println(shopDO.getId()+"/"+shopDO.getName());
}
mongoDB按条件ID查询数据成功,集合为col,文档为:
100/菜鸟教程

1.3 复合条件查询,排序

Criteria criteria = new Criteria();
criteria.orOperator(
Criteria.where("title").is("cainiao"),
Criteria.where("name").is("百易教程"));
Query query = new Query(criteria);   //组合查询放入query
Sort sort = new Sort(new Sort.Order(Sort.Direction.ASC.DESC,"id"));  //结果集进行排序
query.with(sort);
List list2 = mongoTemplate.find(query,ShopDO.class,"col");
System.out.println("mongoDB组合查询数据成功,集合为col,文档为:");
for (ShopDO shopDO:list2){
   System.out.println(shopDO.getId()+"/"+shopDO.getName());
 }
mongoDB组合查询数据成功,集合为col,文档为:
200/百易教程
100/菜鸟教程

1.4 分页条件查询

Query query = new Query();
query.skip(1).limit(3);
List list3 = mongoTemplate.find(query,ShopDO.class,"col");
System.out.println("mongoDB分页查询下标为1开始总共3行数据,集合为col,文档为:");
for (ShopDO shopDO:list3){
   System.out.println(shopDO.getId()+"/"+shopDO.getName());
}
mongoDB分页查询下标为1开始总共3行数据,集合为col,文档为:
101/有道笔记
200/百易教程
201/CSDN教程

db.col.find().skip(M),limit(N) 

1.5 模糊查询 regex,满足条件直接更新

Query query = new Query(Criteria.where("name").regex("教程"));
List list2 = mongoTemplate.find(query,ShopDO.class,"col");
System.out.println("mongoDB查询主键为201数据成功,集合为col,文档为:");
  for (ShopDO shopDO:list2){
        System.out.println(shopDO.getId()+"/"+shopDO.getName());
    }
Update update = new Update();
update.set("name","菜鸟教程old"); 
update.set("title","cainiaoold");
update.set("desc","descold");
WriteResult wr = mongoTemplate.updateMulti(query,update,"col");
System.out.println("mongoDB更新数据成功,集合为col,行数为:" + wr.getN());
mongoDB查询主键为201数据成功,集合为col,文档为:
100/菜鸟教程
201/菜鸟教程old
mongoDB更新数据成功,集合为col,行数为:2

db.col.find(name:{$regex:/教程/})

1.6 更新文档

Query query = new Query(Criteria.where("_id").is(201L));
List list2 = mongoTemplate.find(query,ShopDO.class,"col");
System.out.println("mongoDB查询主键为201数据成功,集合为col,文档为:");
for (ShopDO shopDO:list2){
   System.out.println(shopDO.getId()+"/"+shopDO.getName());
}
Update update = new Update();
update.set("name","菜鸟教程old");
update.set("title","cainiaoold");
update.set("desc","descold");
WriteResult wr = mongoTemplate.updateFirst(query,update,"col");
System.out.println("mongoDB更新数据成功,集合为col,行数为:" + wr.getN());

请注意上图中 id为 "_id" 

mongoDB查询主键为201数据成功,集合为col,文档为:
201/菜鸟教程new
mongoDB更新数据成功,集合为col,行数为:1

1.7 删除文档

Query query = new Query(Criteria.where("_id").is(201L));
WriteResult result = mongoTemplate.remove(query, "col");
System.out.println("mongoDB更新数据成功,集合为col,行数为:" + result.getN() + "更新的ID为"+result.getUpsertedId());

请注意上图中 id为 "_id" 

你可能感兴趣的:(mongoDB)