• 【博学谷学习记录】超强总结,用心分享|架构师-前置知识-mongodb基本使用


    mongodb是非关系型、文档数据库,数据形式Bson

    一、Nosql简介

    1.1 为何要nosql?

    myql扩容时配置要求高、成本高
    nosql自动水平扩容操作简单

    1.2 优点:

    高可扩展性
    分布式计算
    低成本
    架构的灵活性,半结构化数据
    没有复杂的关系

    1.3 缺点:

    没有标准化
    有限的查询功能(到目前为止)
    最终一致是不直观的程序

    二、mongoDB基础

    2.1 存储结构

    MongoDB 将数据存储为一个文档,数据结构由键值(key=>value)对组成。MongoDB 文档类似于
    JSON 对象。字段值可以包含其他文档,数组及文档数组。
    在这里插入图片描述

    2.2 特点

    非关系型数据库,基于 Document data model (文档数据模型)
    MongoDB以 BSON (BinaryJSON) 格式存储数据,类似于 JSON 数据形式
    关系型数据库使用 table (tables of rows)形式存储数据,而MongoDB使用 collections
    (collections of documents)
    支持 临时查询( ad hoc queries ): 系统不用提前定义可以接收的查询类型
    索引通过 B-tree 数据结构, 3.2版本的WiredTiger 支持 log-structured merge-trees(LSM)
    支持索引和次级索引( secondary indexes ): 次级索引是指文档或row有一个 主键( primary key )作为索引,同时允许文档或row内部还拥有一个索引,提升查询的效率,这也是MongoDB比
    较大的一个特点

    2.3 和传统数据库对比

    在mongodb中基本的概念是文档、集合、
    数据库
    在这里插入图片描述

    三、springboot整合mongodb

    3.1 引入坐标

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
    </dependency>
    
    • 1
    • 2
    • 3
    • 4

    3.2 配置

    server:
      port: 8080
    spring:
      application:
        name: spring-boot-test
      data:
        mongodb:
          database: test
          host: 192.168.10.30
          port: 27017
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    3.3 定义实体类

    Blog@Document("blog")
    public class Blog {
        @Id
        private String id;
        private String title;
        private String by;
        private String url;
        private List<String> tags;
        private int likes;
        setter getter ....
    }
    
    DAO类
    @Component
    public class BlogDao {
        @Autowired
        private MongoTemplate mongoTemplate;
    
        public void insert(Blog blog) {
            mongoTemplate.insert(blog);
        }
    
        public Blog findByID(String id) {
            return mongoTemplate.findById(id, Blog.class);
        }
    
    
        public void deleteByID(String id) {
            mongoTemplate.remove(Query.query(Criteria.where("_id").is(id)), Blog.class);
        }
    
        public List<Blog> find(Blog blog) {
            if (null == blog) {
                return null;
            }
            Criteria criteria = getFilter(blog);
            return mongoTemplate.find(Query.query(criteria), Blog.class);
    
        }
    
        public Criteria getFilter(Blog blog) {
            Criteria criteria = new Criteria();
            if (!StringUtils.isEmpty(blog.getTitle())) {
                criteria.andOperator(Criteria.where("title").is(blog.getUrl()));
            }
            if (!StringUtils.isEmpty(blog.getBy())) {
                criteria.andOperator(Criteria.where("by").is(blog.getBy()));
            }
            if (!StringUtils.isEmpty(blog.getLikes())) {
                criteria.andOperator(Criteria.where("likes").is(blog.getLikes()));
            }
            if (null != blog.getTags() && !blog.getTags().isEmpty()) {
                criteria.andOperator(Criteria.where("tags").in(blog.getTags()));
            }
    
            return criteria;
        }
    
    }
    
    • 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

    3.4 controller

    @RestController
    @RequestMapping("/blog")
    public class WebController {
        @Resource
        private BlogDao blogDao;
    
        @RequestMapping("/{id}")
        @ResponseBody
        public String getBlogInfo(@PathVariable("id") String id) {
            Blog blog = blogDao.findByID(id);
            if (null == blog) {
                return "访问的数据不存在";
            }
            return JSON.toJSONString(blog);
        }
    
        @RequestMapping("/add")
        @ResponseBody
        public String addBlog(@RequestBody Blog blog) {
            blogDao.insert(blog);
            return JSON.toJSONString(blog);
        }
    
        public void batchAdd(){
    
        }
    }
    
    • 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

    测试结果
    在这里插入图片描述

  • 相关阅读:
    专访阿里云 RocketMQ 团队:现代微服务架构需要新的消息系统
    图搜索算法详解
    语义分割 U-Net 应用入门
    常见JVM面试题及答案整理
    ROS实现话题的发布与订阅
    【LeetCode】【剑指offer】【反转链表】
    (附源码)springboot企业合同管理系统 毕业设计 161456
    Scrapy框架(四)常用的类概述
    C语言程序设计-8 函 数
    基于MRF和CNN的图像生成
  • 原文地址:https://blog.csdn.net/qq_22043649/article/details/126092157