简介
MongoDB是一个基于分布式文件存储的数据库。由C++语言编写。旨在为WEB应用提供可扩展的高性能数据存储解决方案。
MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。它支持的数据结构非常松散,是类似json的bson格式,因此可以存储比较复杂的数据类型。Mongo最大的特点是它支持的查询语言非常强大,其语法有点类似于面向对象的查询语言,几乎可以实现类似关系数据库单表查询的绝大部分功能,而且还支持对数据建立索引。
docker-compose 模版
1️⃣ 这句 ./mongodb/data:/data/db 是将容器的 /data/db 挂载出来,以后删了容器数据还在
2️⃣ 要记得把端口 27017 给开上
1 2 3 4 5 6 7 8 9 10 11 12
| mongodb: image: mongo container_name: mongodb restart: always environment: MONGO_INITDB_ROOT_USERNAME: admin MONGO_INITDB_ROOT_PASSWORD: HRdwXbj8V0 network_mode: host volumes: - ./mongodb/data:/data/db command: mongod --auth
|
建库建集合
生成权限
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| # 使用 AutoMateX 库 use AutoMateX; # 创建一个 automatex_role 角色,赋予AutoMateX库的增删改查权限 db.createRole({ role: "automatex_role", privileges: [ { resource: { db: "AutoMateX", collection: "" }, actions: ["find", "insert", "update", "remove"] } ], roles: [] }); # 切换到 admin 库(默认自带的) use admin; # 给 admin 账户赋予 automatex_role 角色 db.grantRolesToUser("admin", [{ role: "automatex_role", db: "AutoMateX" }]);
|
SpringBoot配置
1
| implementation("org.springframework.boot:spring-boot-starter-data-mongodb:3.0.5")
|
1 2 3 4
| spring: data: mongodb: uri: mongodb://admin:HRdwXbj8V0@192.168.1.14:27017/AutoMateX?authSource=admin
|
代码实例
@Document(collection = “test_case”) 和 Mybatis 的@TableName 注解相似,用于指定表名(集合)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| package biz.ada.automatex.worker.web.entity; import biz.ada.automatex.worker.web.entity.Assertion.AssertionVO; import biz.ada.automatex.worker.web.entity.ProjectCase.ProjectCaseVO; import biz.ada.automatex.worker.web.entity.ProjectCaseApi.ProjectCaseApiVO; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.springframework.data.mongodb.core.mapping.Document; import java.util.List; import java.util.stream.Collectors;
@Data @AllArgsConstructor @NoArgsConstructor @Document(collection = "test_case") public class TestCase { private Integer id; private ProjectCase projectCase; private ProjectCaseApi projectCaseApi; private List<Assertion> assertions; public record TestCaseVo( ProjectCaseVO projectCaseVO, ProjectCaseApiVO projectCaseApiVO, List<AssertionVO> assertionVo ) { public static List<TestCaseVo> setTestCaseVo(List<TestCase> testCases) { return testCases.stream()
.map(testCase -> new TestCaseVo( ProjectCaseVO.setVo(testCase.getProjectCase()), ProjectCaseApiVO.setVo(testCase.getProjectCaseApi()), AssertionVO.setVo(testCase.getAssertions()) ) ).collect(Collectors.toList()); } } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| package biz.ada.automatex.worker.web.service; import biz.ada.automatex.worker.web.entity.TestCase; import org.springframework.data.mongodb.repository.MongoRepository;
public interface TestCaseRepository extends MongoRepository<TestCase ,String> { }
|
testCaseRepository.saveAll() 是从 MongoRepository 继承过来的,是用于写入 List<>类型的,如果单个对象testCaseRepository.save() 就可以了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| package biz.ada.automatex.worker.web.controller.project; import biz.ada.automatex.starter.annotation.SystemLog; import biz.ada.automatex.starter.response.Result; import biz.ada.automatex.worker.web.mapper.TestCaseMapper; import biz.ada.automatex.worker.web.service.TestCaseRepository; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;
@RestController @RequestMapping("/project/case") @Slf4j public class ProjectCaseController { final TestCaseMapper testCaseMapper; final TestCaseRepository testCaseRepository; public ProjectCaseController(TestCaseMapper testCaseMapper, TestCaseRepository testCaseRepository) { this.testCaseMapper = testCaseMapper; this.testCaseRepository = testCaseRepository; } @SystemLog(BusinessName = "新增或根据主键更新测试计划") @PostMapping("/save/plan") Result saveOrUpdatePlan() { var testCases = testCaseMapper.selectAllTestCase(); testCaseRepository.saveAll(testCases); return Result.ok(); } }
|
Linux 命令行
1️⃣ 描述:服务器上通过命令行进入镜像对应的 MongoDB 容器,后执行查询语句
2️⃣ 今天需要验证线上的问题,连接到现场服务器的 MongoDB 查询里面的数据进行比较验证,但是由于网络原因无法直接通过工具连接,只能进入到线上服务器后通过命令行进行连接验证
1 2
| # 通过关键字 mongo 查询容器对应的 id docker ps | grep mongo
|
1 2
| # 通过 exec -it 进入刚才的查询返回的 id docker exec -it b43310776416 mongo
|
1 2
| // 执行查询 db.getSiblingDB("feature_realtime_service").getCollection("realtime_qc_log_record").find({"realtimeQcRequest.caseNumber": "ada_20230817001"})
|