一.pom引入jar
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
二.配置Mondb数据库连接信息
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
##指定使用的数据库(集合)
spring.data.mongodb.database=article
# 登录认证的逻辑库名
spring.data.mongodb.authentication-database=admin
spring.data.mongodb.password=123456
spring.data.mongodb.username=admin
三.创建对象
import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Transient;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;
/**
* @Author: ws
* @CreateTime: 2023-01-30 09:47
* @Version: 1.0
*/
@Document(collection = "article") //指定要对应的文档名(表名)
@Accessors(chain = true)
@Data
public class User {
@Id
private Integer id;
@Field("userName")
private String name;
@Field("userSex")
private String sex;
@Field("age")
private Integer age;
@Transient
private Integer weight;
public User(Integer id, String name, String sex, Integer age, Integer weight) {
this.id = id;
this.name = name;
this.sex = sex;
this.age = age;
this.weight = weight;
}
}
四.引入工具类
import java.util.regex.Pattern;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
@Autowired
private MongoTemplate mongoTemplate;
五.mongdb操作
1.插入对象
public void addUser() {
//1.操作实体时,如果自己定义了id,新增时必须给id赋值,不然会报错,没有建立id的实体,那么会自动生成id
//2.save方法,如果id存在,就是更新id对应这条数据
User user = new User(1, "张思美1", "女", 18, 110);
mongoTemplate.save(user);
//insert方法会自动生成id,插入的id已存在时,会报错
// mongoTemplate.insert(user);
}
2.插入对象集合
public void addUserList() {
User panda = new User(3, "龟", "动物", 18, 1000);
List<User> users = new ArrayList<>();
users.add(panda);
mongoTemplate.insert(users, User.class);
}
3.插入Map
public void inserMap() {
Map data = new HashMap();
data.put("name", "ws");
data.put("sex", "male");
String date = String.format("%1$tF %1$tT", new Date());
data.put("date", date);
//uu是数据库(集合)名称
mongoTemplate.insert(data, "uu");
}
4.精准查询
public void matchQuery() {
Query query = new Query();
query.addCriteria(Criteria.where("sex").is("male"));
List<Map> data = mongoTemplate.find(query, Map.class, "uu");
System.out.println(JSON.toJSONString(data));
}
5.模糊查询
public void likeQuery() {
String name = "w";
Pattern pattern = Pattern.compile("^.*" + name + ".*$", Pattern.CASE_INSENSITIVE);
Query query = new Query();
query.addCriteria(Criteria.where("name").regex(pattern));
List<Map> data = mongoTemplate.find(query, Map.class, "uu");
System.out.println(JSON.toJSONString(data));
}
6.范围查询
public void betweenQuery() {
Query query = new Query();
query.addCriteria(Criteria.where("date").gt("2019-01-11 14:05:01").lte("2019-01-11 14:09:53"));
List<Map> data = mongoTemplate.find(query, Map.class, "uu");
System.out.println(JSON.toJSONString(data));
}
7.分页查询
分页 limit(size) skip(skipsize)
size:每页几条数据 ; skipsize: 跳过的数据条数
例如:每页size条数据.则第page页的数据为 query.limit(size).skip(size*(page-1))
public void queryListPage(){
Query query = new Query();
query.limit(3).skip(3*(4-1));
List<Map> data = mongoTemplate.find(query,Map.class,"uu");
}
8.综合查询
Query query = new Query();
query.with(new Sort(Sort.Direction.ASC,"date"));
Pattern pattern=Pattern.compile("^.*"+"w"+".*$", Pattern.CASE_INSENSITIVE);
query.addCriteria(Criteria.where("sex").is("male").and("name").regex(pattern).
and("date").gt("2019-01-11 14:05:01").lte("2019-01-11 14:09:53"));
List<Map> data = mongoTemplate.find(query,Map.class,"uu");
9.单条查询
Map data = mongoTemplate.findOne(query,Map.class,"uu");
10.删除
Query query=new Query(Criteria.where("date").is("2019-01-11 14:14:05"));
long count = mongoTemplate.remove(query,Map.class,"uu").getDeletedCount();
11.更新
Query query=new Query(Criteria.where("date").is("2019-01-11 14:13:38"));
Update update = Update.update("name","hhd").set("sex","animal");
mongoTemplate.updateFirst(query, update, Map.class,"uu");