转载自:https://blog.csdn.net/sinat_35821285/article/details/83511203#commentBox
1.首先pom.xml
<!-- springboot 整合 mongodb --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> <version>2.0.0.RELEASE</version> </dependency>
引入
@Autowired private MongoTemplate mongoTemplate;
插入数据(可以是一个对象,或者是一个对象集合)
List<SysUser> userList = new ArrayList<>(); userList.add(new SysUser(11,"1","白小飞","18888888888")); userList.add(new SysUser(9,"1","白敬亭","13333333333")); userList.add(new SysUser(5,"1","林宥嘉","14444444444")); userList.add(new SysUser(6,"1","彭于晏","15555555555")); userList.add(new SysUser(7,"1","周杰伦","16666666666")); mongoTemplate.insert(userList, "userList");
SysUser sysUser = new SysUser(12,"1","黄小飞","18888888888"); mongoTemplate.insert(sysUser, "userList");
当存入的第一个数据类型是SysUser,名字是userList的时候,第二个存入数据就必须是SysUser类型的数据。
查询数据
查询单个数据和多个数据,模糊查询
//查询单个数据 public SysUser findMongo() { Pattern pattern = Pattern.compile("^.*8$",Pattern.CASE_INSENSITIVE); Query query = new Query(Criteria.where("phone").regex(pattern)); SysUser findOne = mongoTemplate.findOne(query,SysUser.class,"userList"); return findOne; }
//查询多个数据 public List<SysUser> findListMongo() { //模糊查询以 ^开始 以$结束 .*相当于Mysql中的% Pattern pattern = Pattern.compile("^1.*$",Pattern.CASE_INSENSITIVE); Query query = new Query(Criteria.where("phone").regex(pattern)); // Query query = new Query(Criteria.where("status").is("1")); // query.with(new Sort(Direction.DESC,"phone")); //按手机号码倒序 List<SysUser> findList = mongoTemplate.find(query,SysUser.class,"userList"); return findList; }
//多个条件查询(格式2) public List<SysUser> findList2(){ Criteria criteria = new Criteria(); criteria.and("name").is("秦岚"); criteria.and("phone").is("12222222222"); Query query = new Query(criteria); List<SysUser> findList = mongoTemplate.find(query, SysUser.class,"userList"); return findList; }
//一个模糊关键字匹配多个字段 public List<SysUser> findList3(){ Pattern pattern = Pattern.compile("^.*222$",Pattern.CASE_INSENSITIVE); Criteria criteria = new Criteria(); //phone以222结尾的 或者 name以222结尾的 criteria.orOperator(Criteria.where("phone").regex(pattern), Criteria.where("name").regex(pattern)); //同时满足 phone以222结尾的,和name以222结尾的 criteria.andOperator(Criteria.where("phone").regex(pattern), Criteria.where("name").regex(pattern)); Query query = new Query(criteria); List<SysUser> find = mongoTemplate.find(query, SysUser.class,"userList"); return find; }
更新。
//更新 public int update() { Query query = new Query(); query.addCriteria(Criteria.where("_id").is(1)); //_id区分引号 "1"和1 Update update = Update.update("name", "zzzzz"); // WriteResult upsert = mongoTemplate.updateMulti(query, update, "userList"); //查询到的全部更新 // WriteResult upsert = mongoTemplate.updateFirst(query, update, "userList"); //查询更新第一条 WriteResult upsert = mongoTemplate.upsert(query, update, "userList"); //有则更新,没有则新增 return upsert.getN(); //返回执行的条数 }
//添加内嵌文档数据(有则直接加入,没有则进行新增) public int update1() { Query query = Query.query(Criteria.where("_id").is("11")); SysUser user = new SysUser(1,"1","lisi","19999998745"); Update update = new Update(); update.addToSet("users", user); WriteResult upsert = mongoTemplate.upsert(query, update, "userList"); return upsert.getN(); }
//修改内嵌文档中数据 public int update2() { //查询_id为11并且其中userList文档的_id为1的 Query query = Query.query(Criteria.where("_id").is("11").and("users._id").is(1)); Update update = Update.update("users.$.name", "zhangsan"); WriteResult upsert = mongoTemplate.upsert(query, update, "userList"); return upsert.getN(); }
//删除内嵌文档中数据 public int delete() { //查询_id为11并且其中userList文档的_id为1的 Query query = Query.query(Criteria.where("_id").is("11").and("users._id").is(1)); Update update = new Update(); update.unset("users.$"); WriteResult upsert = mongoTemplate.updateFirst(query, update, "userList"); return upsert.getN(); }
查询多个数据删除之后是这样的: