该栏目讲叙 MongoDB 相关的知识,包括简介、各模块的操作和与 Spring 的整合
MongoTemplate 相关API
1、常用方法
描述 | 方法 |
---|
添加文档 | insert() 、save() |
更新文档 | update() |
删除文档 | remove() |
查询文档 | findOne() 、findAll() 、find() |
聚合 | aggregate() |
2、常用类
描述 | 方法 |
---|
创建对象 | query(Criteria criteria) |
限制查询数量 | limit(int limit) |
跳转 | skip(int skip) |
添加分页或排序 | with(Page page | Sort sort) |
描述 | 方法 |
---|
where条件 | where(String key) |
相等 | is(String value) |
大于 | gt(String value) |
小于 | lt(String value) |
与 | and(Criteria criteria) |
或 | or(Criteria criteria) |
描述 | 方法 |
---|
创建对象 | Sort.Order.desc(key) |
描述 | 方法 |
---|
创建对象 | new Aggregation(AggregationOperation op) |
描述 | 方法 |
---|
类似于where | math(Criteria criteria) |
分组 | group(String key) |
统计 | count() |
最大值 | max() |
最小值 | min() |
平均值 | avg() |
别名 | as(String alias) |
整合 MongoDB
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
spring:
data:
mongodb:
database: test
host: 127.0.0.1
authentication-database: admin
@Document
@Data
@ToString
public class User {
@Id
private String id;
private String name;
private int age;
private boolean gender;
}
@SpringBootTest(classes = MongoApplication.class)
public class UserOperationTest {
@Autowired
private MongoTemplate mongoTemplate;
@Test
public void insertTest() {
User user = new User();
user.setName("Near");
user.setAge(18);
user.setGender(true);
final User dbUser = mongoTemplate.insert(user);
System.out.println("用户信息:" + dbUser);
}
@Test
public void saveTest() {
User user = new User();
user.setId("608c403db147fe3647a61ef0");
user.setName("Near2");
user.setAge(20);
final User dbUser = mongoTemplate.save(user);
System.out.println("用户信息:" + dbUser);
}
@Test
public void updateTest() {
Update update = BasicUpdate.update("name", "lisi2").set("age", 28);
final UpdateResult result = mongoTemplate.updateFirst(
Query.query(Criteria.where("name").is("lisi")), update, User.class);
System.out.println("更新结果:" + result);
}
@Test
public void removeTest() {
final DeleteResult result = mongoTemplate.remove(Query.query(Criteria
.where("name").is("lisi2")), User.class);
System.out.println("删除结果:" + result);
}
@Test
public void findTest() {
final User user = mongoTemplate.findOne(Query.query(Criteria.where("name")
.is("Near2")), User.class);
System.out.println("用户信息:" + user);
System.out.println("=============================");
List<User> users = mongoTemplate.findAll(User.class);
users.forEach(System.out::println);
System.out.println("=============================");
final Criteria criteria = Criteria.where("age").gt(15).lt(22);
users = mongoTemplate.find(Query.query(criteria).with(Sort.by(Sort.Order.asc("age")))
, User.class);
users.forEach(System.out::println);
System.out.println("=============================");
final long count = mongoTemplate.count(Query.query(Criteria.where("age").gte(18)), User.class);
System.out.println("成年用户的个数:" + count);
System.out.println("=============================");
final PageRequest pageRequest = PageRequest.of(1, 3);
final Query query = Query.query(Criteria.where("name").is("zhangsan"));
users = mongoTemplate.find(query.with(pageRequest), User.class);
long total = mongoTemplate.count(query, User.class);
PageImpl<User> userPage = new PageImpl<>(users, pageRequest, total);
System.out.println("分页信息:" + userPage);
System.out.println("=============================");
List<AggregationOperation> operations = new ArrayList<>();
operations.add(Aggregation.match(Criteria.where("name").is("zhangsan")));
operations.add(Aggregation.group("age").count().as("count"));
final AggregationResults<Result> result = mongoTemplate.aggregate(
Aggregation.newAggregation(operations), "user", Result.class);
System.out.println("统计结果" + result.getMappedResults());
}
}
@Data
@AllArgsConstructor
class Result {
private String id;
private int count;
}
所有评论(0)