目录
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
spring:
data:
mongodb:
uri: mongodb://192.168.73.3:27017/demo
# mongodb 一般不设置密码
# username: root
# password: 1111
uri 格式为: mongodb://ip 地址:mongodb 端口号/集合名
Ps:以 demo 集合为例
a)创建集合
if(!mongoTemplate.collectionExists("demo")) {
//不存在才能创建,如果以及存在再创建就会报错
mongoTemplate.createCollection("demo");
}
b)删除集合
mongoTemplate.dropCollection("demo1");
a)@Document
b)@Id
c)@Field
d)@Transient
以 User 类为例:
@Document("demo") //表示当前文档属于哪个集合
public class User {
@Id //当前类的 id 映射文档中的 _id
private Integer id;
private String name;
private Integer age;
@Field("work_day") //当前类的 workDay 映射文档中的 work_day
private Date workDay;
public User(Integer id, String name, Integer age, Date workDay) {
this.id = id;
this.name = name;
this.age = age;
this.workDay = workDay;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", workDay=" + workDay +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Date getWorkDay() {
return workDay;
}
public void setWorkDay(Date workDay) {
this.workDay = workDay;
}
}
//1.查询所有
System.out.println("------------------------------------------------");
List<User> users = mongoTemplate.findAll(User.class);
users.forEach(System.out::println);
//2.根据 id 查询指定文档
System.out.println("------------------------------------------------");
User byId = mongoTemplate.findById(1, User.class);
System.out.println(byId);
//3.根据查询条件进行查询(参数1: 查询条件, 参数2: 返回类型)
System.out.println("------------------------------------------------");
// mongoTemplate.find(new Query(), User.class); //没有查询条件就是查询所有
//a) 等值查询
System.out.println("------------------------------------------------");
List<User> users1 = mongoTemplate.find(Query.query(Criteria.where("name").is("周杰伦")), User.class);
users1.forEach(System.out::println);
//b) > 查询: gt()、>= 查询: gte() 、 < 查询: lt()、<= 查询 lte() 查询
//以 > 为例
System.out.println("------------------------------------------------");
List<User> users2 = mongoTemplate.find(Query.query(Criteria.where("age").gt(30)), User.class);
users2.forEach(System.out::println);
//4.and 查询
System.out.println("------------------------------------------------");
List<User> users3 = mongoTemplate.find(Query.query(Criteria.where("name").is("薛之谦").and("age").is(40)), User.class);
users3.forEach(System.out::println);
//5.or 查询
System.out.println("------------------------------------------------");
Criteria criteria = new Criteria();
criteria.orOperator(
Criteria.where("name").is("周杰伦"),
Criteria.where("name").is("薛之谦")
);
List<User> users4 = mongoTemplate.find(Query.query(criteria), User.class);
users4.forEach(System.out::println);
//6.and 和 or
System.out.println("------------------------------------------------");
Criteria criteria1 = new Criteria();
criteria1.and("age").is(40)
.orOperator(
Criteria.where("name").is("周杰伦"),
Criteria.where("name").is("薛之谦")
);
List<User> users5 = mongoTemplate.find(Query.query(criteria1), User.class);
users5.forEach(System.out::println);
//7.sort
System.out.println("------------------------------------------------");
Query query = new Query();
query.with(Sort.by(Sort.Order.desc("age"))); //desc 降序, asc 升序
mongoTemplate.find(query, User.class);
//8.分页: skip limit
System.out.println("------------------------------------------------");
Query queryPage = new Query();
queryPage.with(Sort.by(Sort.Order.desc("age"))) //desc 降序, asc 升序
.skip(0)
.limit(3);
mongoTemplate.find(queryPage, User.class);
//9.总数
System.out.println("------------------------------------------------");
mongoTemplate.count(new Query(), User.class);
//10.去重 distinct(参数1: 查询条件, 参数2: 去重字段, 参数3: 操作集合, 参数4: 返回类型)
System.out.println("------------------------------------------------");
List<String> name = mongoTemplate.findDistinct(new Query(), "name", User.class, String.class);
//11.json 字符串查询
System.out.println("------------------------------------------------");
Query queryJson = new BasicQuery("{name: '周杰伦', age: '20'}");
List<User> users6 = mongoTemplate.find(queryJson, User.class);
//1.更新条件
Query query = Query.query(Criteria.where("name").is("周杰伦"));
//2.更新内容
Update update = new Update();
update.set("age", 20);
//a) 更新单条
mongoTemplate.updateFirst(query, update, User.class);
//b) 更新多条
mongoTemplate.updateMulti(query, update, User.class);
//c) 不存在就插入(存在就更新第一条)
mongoTemplate.upsert(query, update, User.class);
//1.删除所有
mongoTemplate.remove(new Query(), User.class);
//2.条件删除
mongoTemplate.remove(Query.query(Criteria.where("name").is("林俊杰")), User.class);