• mongodb(快速上手)(一)


    资料地址:

    链接:https://pan.baidu.com/s/1DxgX3RCvWfaKzziHG8IHZQ
    提取码:3452

    gitee地址

    https://gitee.com/liushanshan126/mongodb-test

    一、mongdb相关概念

    1、mysql和mongdb对比

    在这里插入图片描述
    在这里插入图片描述

    2、bson(mongdb存储结构,与json类似)

    1. Bson中,除了基本的JSON类型:string,integer,boolean,double,null,array和object
    2. mongo还使用了特殊的数据类型。这些类型包括date,object id,binary data,regular expression
      和code
      。每一个驱动都以特定语言的方式实现了这些类型,查看你的驱动的文档来获取详细信息。
      在这里插入图片描述

    二、单机部署

    1、安装压缩文件,并且创建data/db文件

    2、单机启动命令

    进入bin文件夹中,cmd控制台,输入命令:

    mongod–dbpath=…\data\db

    三、基本常用命令

    1、创建、显示数据库

    在这里插入图片描述
    注意:创建数据库,但是这个数据库没有集合(表)的时候,只会存在于内存中,只有当当前的数据库中有集合的时候才会实例化到磁盘中

    2、删除

    进入需要删除的数据库,然后执行

    db.dropDatabase()

    3 、插入数据

    db.comment.insert({“articleid”:“100000”,“content”:“今天天气真好,阳光明媚”,“userid”:“1001”,“nickname”:“Rose”,“createdatetime”:newDate(),“likenum”:NumberInt(10),“state”:null})

    3、查询db.document.find({},{}):document为表;第一个大括号:查询条件,相当于where后面的条件;第二个大括号:显示哪些字段,相当于select后面的查询字段

    例如: db.article.findOne({articleid:‘12222’}, {userid:1, nickname:1}) 查询字段articleid为12222的数据,且只拿出userid,nickname, _id(默认)字段

    4、更新

    4.1 、 普通更新(局部更新$set)

    db.article.update({_id:“2”},{$set:{likenum:NumberInt(889)}})

    4.2 列值增加 (数字 + n) $inc

    db.article.update({_id:“3”},{$inc:{likenum:NumberInt(1)}})

    5、删除文档

    在这里插入图片描述

    6、排序 sort

    sort() 方法可以通过参数指定排序的字段,并使用 1 和 -1 来指定排序的方式,其中 1 为升序排列,而 -1 是用于降序排列。

    db.COLLECTION_NAME.find().sort({KEY:1})

    skip、limit、sort的执行顺序

    跟mysql一样,先执行sort,在执行skip,最后执行limit

    7、正则表达

    在这里插入图片描述

    8、比较查询(大于,小于,大于等于,小于等于,不等于)

    在这里插入图片描述

    9、包含(in),不包含(nin)

    in:db.comment.find({userid:{KaTeX parse error: Expected 'EOF', got '}' at position 19: …["1003","1004"]}̲}) nin:db.comme…nin:[“1003”,“1004”]}})

    10、条件连接。or和and的使用

    在这里插入图片描述

    总结

    选择切换数据库:usearticledb
    插入数据:db.comment.insert({bson数据})
    查询所有数据:db.comment.find();
    条件查询数据:db.comment.find({条件})
    查询符合条件的第一条记录:db.comment.findOne({条件})
    查询符合条件的前几条记录:db.comment.find({条件}).limit(条数)
    查询符合条件的跳过的记录:db.comment.find({条件}).skip(条数)
    修改数据:db.comment.update({条件},{修改后的数据})或db.comment.update({条件},{KaTeX parse error: Expected '}', got 'EOF' at end of input: …t.update({条件},{inc:{自增的字段:步进值}})
    删除数据:db.comment.remove({条件})
    统计查询:db.comment.count({条件})
    模糊查询:db.comment.find({字段名:/正则表达式/})
    条件比较运算:db.comment.find({字段名:{$gt:值}})

    包含查询:db.comment.find({字段名:{KaTeX parse error: Expected 'EOF', got '}' at position 11: in:[值1,值2]}̲})或db.comment.f…nin:[值1,值2]}})
    条件连接查询:db.comment.find({KaTeX parse error: Expected 'EOF', got '}' at position 18: …d:[{条件1},{条件2}]}̲)或db.comment.fi…or:[{条件1},{条件2}]})

    四、索引

    1、类型

    单字段索引、复合索引、其他索引

    2、索引的查看、创建、删除

    查看:db.collection.getIndexes()

    创建:
    单字段索引:db.comment.createIndex({userid:1})
    复合索引:db.comment.createIndex({userid:1,nickname:-1})

    删除:
    单个删除:db.comment.dropIndex({userid:1})
    删除所有索引:db.collection.dropIndexes()

    3、使用索引作为条件,查询字段也为索引,则不查集合,直接查询索引

    在这里插入图片描述

    五、java实际操作mongodb

    1、引入依赖

    <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.6.RELEASE</version>
            <relativePath/>
            <!--lookupparentfromrepository-->
        </parent>
    
    <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <!--连接mongodb的依赖-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-mongodb</artifactId>
            </dependency>
        </dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    2、dao

    在这里插入图片描述

    3、实体类

    在这里插入图片描述

    4、service

    package com.bear.service;
    
    import com.bear.dao.CommentRepository;
    import com.bear.pojo.Comment;
    import org.springframework.stereotype.Service;
    
    import javax.annotation.Resource;
    import java.util.Optional;
    
    /**
     * <简述>
     * <详细描述>
     *
     * @author LiuShanshan
     * @version $Id$
     */
    @Service
    public class CommentService {
        @Resource
        private CommentRepository commentRepository;
    
        /**
         *<简述>  增加
         *<详细描述>
         * @author Liushanshan
         * @param comment
         * @return void
        */
        public void saveComment(Comment comment){
            Comment save = commentRepository.save(comment);
            System.out.println(save);
        }
    
        // 删
        public void delete(String id){
            commentRepository.deleteById(id);
        }
    
        // 改
    
        public void update (Comment comment){
            commentRepository.save(comment);
        }
    
        // 查
        public Comment select (String id){
            Optional<Comment> byId = commentRepository.findById(id);
            Comment comment = byId.get();
            return comment;
        }
    }
    
    
    • 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

    5、测试类

    @RunWith(SpringRunner.class)
    @SpringBootTest(classes=MongodbApplication.class)
    public class Test {
        @Autowired
        private CommentService commentService;
    
        // 保存
        @org.junit.Test
        public void saveTest(){
            Comment comment = new Comment();
            comment.setArticleid("100000");
            comment.setContent("测试添加的数据");
            comment.setCreatedatetime(LocalDateTime.now());
            comment.setUserid("1003");
            comment.setNickname("凯撒大帝");
            comment.setState("1");
            comment.setLikenum(0);
            comment.setReplynum(0);
            commentService.saveComment(comment);
        }
    
        // 查询
        @org.junit.Test
        public void select(){
            Comment select = commentService.select("62bd78cc4e03471050ba849e");
            System.out.println(select);
        }
    
        // 更新
        @org.junit.Test
        public void update(){
            Comment select = commentService.select("62bd78cc4e03471050ba849e");
            select.setUserid("234234234");
            commentService.update(select);
        }
    
        // 删除
    
    }
    
    • 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

    6、分页条件查询

    在这里插入图片描述

    6.1 分页条件查询2.0

    在这里插入图片描述

    6.2 MongoTemplate的使用(***)

    在这里插入图片描述

  • 相关阅读:
    OpenCV实战(20)——图像投影关系
    自古以来,反射也是兵家必争之地
    Docker registry 你有深入的玩过吗?
    《PostgreSQL与NoSQL:合作与竞争的关系》
    8月!优选国产软件 - 国货之光 / Windows 系统必备软件大捆绑!
    使用TS 封装 自定义hooks,实现不一样的 CRUD
    《C++避坑神器·十八》运算符重载,小白也能看懂
    什么是腾讯云云硬盘?有哪些优势?应用于哪些场景?
    SSH服务
    uni-app实现图片预览
  • 原文地址:https://blog.csdn.net/M1275601161/article/details/125529061