org.springframework.boot
spring-boot-starter-data-mongodb
2.0.0.RELEASE
@Autowired
private MongoTemplate mongoTemplate;
List 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() {
Query query = new Query();
if (fileid!=null&&!fileid.isEmpty()) {
query.addCriteria(Criteria.where("id").is(fileid));
}
SysUser findOne= mongoTemplate.findone(query, FileModel.class,"oa_file");
return findOne;
}
//查询多个数据
public List findListMongo() {
Query query = new Query();
if (fileid!=null&&!fileid.isEmpty()) {
query.addCriteria(Criteria.where("id").is(fileid));
}
List findList = mongoTemplate.find(query, FileModel.class,"oa_file");
return findList;
}
//多个条件查询(格式1)
public List findList(){
Query query = new Query();
query.addCriteria(Criteria.where("name").is("秦岚"));
query.addCriteria(Criteria.where("phone").is("12222222222"));
List findList = mongoTemplate.find(query, SysUser.class,"userList");
return findList;
}
//多个条件查询(格式2)
public List findList2(){
Criteria criteria = new Criteria();
criteria.and("name").is("秦岚");
criteria.and("phone").is("12222222222");
Query query = new Query(criteria);
List findList = mongoTemplate.find(query, SysUser.class,"userList");
return findList;
}
//一个模糊关键字匹配多个字段
public List 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 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();
}