package com.lhyt.mongodb.controller; import com.lhyt.mongodb.entity.UserEntity; import com.mongodb.client.result.DeleteResult; import com.mongodb.client.result.UpdateResult; import org.bson.Document; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.aggregation.Aggregation; import org.springframework.data.mongodb.core.aggregation.AggregationResults; import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.mongodb.core.query.Update; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; /** * @author zhangYQi */ @RestController @RequestMapping(value = "practice") public class PracticeMongoDBController { @Autowired private MongoTemplate mongoTemplate; /** * 在user表中新增一条数据, * ※ 注意字段名,返回值类型 * * @return */ @RequestMapping(value = "getUser") public UserEntity getUser() { UserEntity userEntity = new UserEntity(); userEntity.setUserName("zyq+++"); userEntity.setPassword("zyq123+"); userEntity.setEmail("zyq111@qq"); userEntity.setAge(777); userEntity.setName("zyq name "); UserEntity userEntity7 = mongoTemplate.save(userEntity, "user"); return userEntity7; } /** * 查询user表中的所有数据 * * @return */ @RequestMapping(value = "queryAll") public List<UserEntity> query() { List<UserEntity> queryAll = mongoTemplate.findAll(UserEntity.class, "user"); return queryAll; } /** * 根据条件查询 */ @RequestMapping(value = "queryFind") public List<UserEntity> queryFind() { Query query = Query.query(Criteria.where("userName").is("zyq888")); List<UserEntity> queryFinds = mongoTemplate.find(query, UserEntity.class, "user"); return queryFinds; } /** * 根据条件查询,并删除查询结果 * ※ mongoTemplate点的是findAndRemove 不是 findAllRemove */ @RequestMapping(value = "findAndRemove") public UserEntity findAndRemove() { Query query = Query.query(Criteria.where("userName").is("zyq999")); UserEntity userEntity = mongoTemplate.findAndRemove(query, UserEntity.class, "user"); return userEntity; } /** * 删除查询出来的这一行,返回删除成功数值1 * 返回值类型:DeleteResult */ @RequestMapping(value = "delete") public DeleteResult deleteUser() { Query query = Query.query(Criteria.where("userName").is("zyq00")); DeleteResult userEntity = mongoTemplate.remove(query, UserEntity.class, "user"); return userEntity; } /** * 根据条件查行,有查询结果更改userName为zyq7890,有结果没字段不做操作 * * @return */ @RequestMapping(value = "modify") public UserEntity modify() { Query query = Query.query(Criteria.where("userName").is("zyq770")); Update update = new Update(); update.set("userName", "zyq7890"); UserEntity userEntity = mongoTemplate.findAndModify(query, update, UserEntity.class, "user"); return userEntity; } /** * 更改查询结果的第一条 */ @RequestMapping(value = "findFirst") public UpdateResult findFirst() { Query query = Query.query(Criteria.where("userName").is("zyq888")); Update update = new Update(); update.set("password", "zyq6789"); UpdateResult userEntity = mongoTemplate.updateFirst(query, update, UserEntity.class, "user"); return userEntity; } /** *根据条件查询,将查询结果中的password都改为zyq77788999 */ @RequestMapping(value = "findMulti") public UpdateResult findMulti() { Query query = Query.query(Criteria.where("userName").is("zyq888")); Update update = new Update(); update.set("password", "zyq777888999"); UpdateResult userEntity = mongoTemplate.updateMulti(query, update, UserEntity.class, "user"); return userEntity; } /** * 聚合,分组 * 条件和分组参数以逗号结尾,回显字段(最后一行没有逗号) * sum 求和, avg 分组平均值 * @return */ @RequestMapping(value = "aggregation") public List<Document> aggregate(){ Aggregation aggregation = Aggregation.newAggregation( Aggregation.match(Criteria.where("userName").is("zyq888")), //条件 Aggregation.group("userName").count().as("sumCount").sum("age").as("sumAge"), //分组参数 Aggregation.project("userName", "sumCount", "sumAge") // 回显字段 ); AggregationResults<Document> userEntity = mongoTemplate.aggregate(aggregation, "user", Document.class); List<Document> aggregeter = userEntity.getMappedResults(); return aggregeter; } }
聚合除求和以外,其他几种需求的写法(将上放sum更换成avg即可):