1.MongoDB 安装
1.1 基于Docker安装
docker run --restart=always -d --name mongo -v /opt/mongodb/data:/data/db -p 27017:27017 mongo:4.0.6
1.2 客户端工具使用
MongoDB Compass | MongoDB
2.MongoDB 使用
2.1 引用依赖包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
2.2 配置文件配置mongodb资料
# MongoDB连接信息
spring.data.mongodb.host = 192.168.23.27
spring.data.mongodb.port = 27017
spring.data.mongodb.database = mallspring.data.mongodb.auto-index-creation = true
2.3 准备对象Person
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Document(collection = "205_person")
//用于指定集合,如果不指定则会默认集合为当前类名
public class Person {
// @Id //用于指定Id,若有该字段,则mongodb默认为model中的id为MongoDB中的id
private Integer id ;
private String name;
private Integer age;
private String phone;
@Indexed(expireAfterSeconds = 10) //必须结合spring.data.mongodb.auto-index-creation = true和@Document注解一起使用
private LocalDateTime creatTime;
}
2.4新增文档
package com.by;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.by.model.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.mongodb.core.MongoTemplate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
@SpringBootTest
class InsertTests {
@Autowired
private MongoTemplate mongoTemplate;
@Test
void test() {
Person person = Person.builder().id(1).name("张").age(18).phone("188744456223").build();
mongoTemplate.insert(person);
}
@Test//指定集合名称进行插入205_person
void test2() {
Person person = Person.builder().id(2).name("张").age(18).phone("188744456223").build();
mongoTemplate.insert(person,"205_person");
}
@Test//指定集合名称进行插入205_person
void test3() {
Person person = Person.builder().id(3).name("张").age(18).phone("188744456223").build();
mongoTemplate.insert(person,"205_person");
}
@Test
void test4() {
Person person = Person.builder().id(2).name("张").age(18).phone("188744456223").build();
mongoTemplate.save(person,"205_person");
}
@Test //批量插入
void test5() {
List<Person> list =new ArrayList<>();
for (int i = 1; i <= 10; i++) {
Person person = Person.builder().id(i).name("张"+i).age(i).phone("188744456223").build();
list.add(person);
}
mongoTemplate.insertAll(list);
}
@Test //测试索引的过期时间
void test6() {
// Person person = Person.builder().id(11).name("张").age(38).phone("188744456223").creatTime(LocalDateTime.now()).build();
// mongoTemplate.save(person);
}
@Test //测试索引的过期时间
void test8() {
// Person person = Person.builder().id(112).name("张").age(38).phone("188744456223").creatTime(LocalDateTime.now()).build();
// mongoTemplate.insert(person);
}
@Test //使用save命令
void test7() {
Person person = Person.builder().id(1).name("张--大爷").age(38).phone("188744456223").build();
mongoTemplate.save(person,"person");
}
}
2.5修改文档
package com.by;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.by.model.Person;
import com.mongodb.client.result.UpdateResult;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.mongodb.core.MongoTemplate;
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 java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
@SpringBootTest
class UpdateTests {
@Autowired
private MongoTemplate mongoTemplate;
@Test //更新满足条件的第一条数据
void test() {
Query query =new Query(Criteria.where("id").is(1));
Update update=new Update().set("age",38);
mongoTemplate.updateFirst(query,update,Person.class);
}
@Test //批量更新年龄大于10的
void test2() {
Query query =new Query(Criteria.where("age").gte(10));
Update update=new Update().set("age",38);
mongoTemplate.updateMulti(query,update,Person.class);
}
@Test //使用正则表达式修改姓张的年龄
void test3() {
Query query =new Query(Criteria.where("name").regex("^张"));
Update update=new Update().set("age",48);
UpdateResult updateResult = mongoTemplate.updateMulti(query, update, Person.class);
}
}
2.6删除文档
package com.by;
import com.by.model.Person;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
@SpringBootTest
class DeleteTests {
@Autowired
private MongoTemplate mongoTemplate;
@Test //删除满足条件的第一条数据
void test() {
Query query =new Query(Criteria.where("id").is(2));
DeleteResult remove = mongoTemplate.remove(query,Person.class);
}
@Test //删除满足条件的第一条数据
void test2() {
Query query =new Query(Criteria.where("name").regex("3$"));
DeleteResult remove = mongoTemplate.remove(query,Person.class);
}
@Test //删除满足条件的第一条数据
void test3() {
Query query =new Query(Criteria.where("id").in(4,5,6));
DeleteResult remove = mongoTemplate.remove(query,Person.class);
}
@Test //删除满足条件的数据
void test4() {
Query query =new Query(Criteria.where("id").in(7,8,9));
DeleteResult remove = mongoTemplate.remove(query,Person.class,"205_person");
}
@Test //删除满足条件的数据
void test5() {
Query query =new Query(Criteria.where("name").regex("^张"));
DeleteResult remove = mongoTemplate.remove(query,Person.class,"205_person");
}
}
2.7查询文档
package com.by;
import com.by.model.Person;
import com.mongodb.client.result.UpdateResult;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
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 java.util.List;
@SpringBootTest
class SelectTests {
@Autowired
private MongoTemplate mongoTemplate;
@Test //查询所有数据
void test() {
List<Person> all = mongoTemplate.findAll(Person.class);
}
@Test //查询指定数据
void test2() {
Query query =new Query(Criteria.where("name").is("张3"));
List<Person> list = mongoTemplate.find(query, Person.class);
}
@Test //查询所有数据
void test3() {
Query query =new Query(Criteria.where("name").is("张3"));
List<Person> list = mongoTemplate.find(query, Person.class,"205_person");
}
@Test //查询所有数据
void test4() {
Query query =new Query(Criteria.where("name").is("张3"));
List<Person> allAndRemove = mongoTemplate.findAllAndRemove(query, Person.class);
}
@Test //查询所有数据
void test5() {
Query query =new Query(Criteria.where("age").gte(2).lte(5));
List<Person> list = mongoTemplate.find(query, Person.class);
}
@Test //用and关联多个查询条件
void test6() {
Criteria name =new Criteria("name").regex("^张");
Criteria age = new Criteria("age").gte(5);
//创建条件对象,用and 进行关联
Criteria criteria =new Criteria().andOperator(name,age);
Query query=new Query(criteria);
List<Person> list = mongoTemplate.find(query, Person.class);
}
@Test //用or关联多个查询条件
void test7() {
Criteria name =new Criteria("name").regex("^张");
Criteria age = new Criteria("age").gte(5);
//创建条件对象,用and 进行关联
Criteria criteria =new Criteria().orOperator(name,age);
Query query=new Query(criteria);
List<Person> list = mongoTemplate.find(query, Person.class);
}
@Test //用in关联多个查询条件
void test8() {
Query query=new Query(Criteria.where("age").in(4,5,6));
List<Person> list = mongoTemplate.find(query, Person.class);
}
@Test //使用正则表达式
void test9() {
Query query=new Query(Criteria.where("name").regex("7$"));
List<Person> list = mongoTemplate.find(query, Person.class);
}
@Test //区间查询
void test10() {
Query query=new Query(Criteria.where("age").gte(5).lte(8));
List<Person> list = mongoTemplate.find(query, Person.class);
}
@Test //降序排序
void test11() {
Query query=new Query(Criteria.where("age").gte(5).lte(8)).with(Sort.by(Sort.Order.desc("age")));
List<Person> list = mongoTemplate.find(query, Person.class);
}
@Test //降序排序
void test12() {
Query query =new Query( Criteria.where("age").gte(5).lte(8)).with(Sort.by(Sort.Order.asc("age")));
List<Person> list = mongoTemplate.find(query, Person.class);
}
}
2.8创建索引
package com.by;
import com.by.model.Person;
import com.mongodb.client.ListIndexesIterable;
import com.mongodb.client.model.Indexes;
import com.mongodb.client.result.UpdateResult;
import org.bson.Document;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
@SpringBootTest
class IndexTests {
@Autowired
private MongoTemplate mongoTemplate;
@Test //更新满足条件的第一条数据
void test() {
//创建升序索引
String index = mongoTemplate.getCollection("205_person").createIndex(Indexes.ascending("name"));
//创建降序索引
String name = mongoTemplate.getCollection("205_person").createIndex(Indexes.descending("name"));
}
@Test //删除索引
void test2() {
mongoTemplate.getCollection("205_person").dropIndex("name_-1");
}
@Test //查看所有索引
void test3() {
ListIndexesIterable<Document> documents = mongoTemplate.getCollection("205_person").listIndexes();
for (Document document :documents) {
System.out.println("索引列表"+document);
}
}
}