• 11.Mogodb数据库基本使用


    1.NoSql概念

    • 1.非结构型数据库,没有行、列的概念。
    • 2.用json来存储数据
    • 3.集合相当于
    • 4.文档相当于
      在这里插入图片描述

    2.Mongodb基础命令

    1.数据库
    • 1.查看数已存在据库

      • show dbs;
        在这里插入图片描述
    • 2.创建数据库

      • use dbname;
        在这里插入图片描述
        在这里插入图片描述

        如果dbname已经存在,则会切换到该数据库
        如果dbname不存在,则会创建临时数据库,只有在创建集合时,才会真正创建该数据库

    • 3.切换数据库

      • use dbname;
        在这里插入图片描述
    • 4.查看当前所在数据库

      • db;
    • 5.删除数据库(需要先切换到要删除的数据库)

      • db.dropDatabase();
        在这里插入图片描述
    2. 集合
    • 1.查看所有集合
      • show collections
        在这里插入图片描述
    • 2.创建集合(两种方式都可以)
      • db.createCollection(collectionName[, options]);
        在这里插入图片描述
      • db.collectionName.insert({"name": "jack"})
        在这里插入图片描述
    • 3.删除集合
      • db.collection.drop();
        在这里插入图片描述
    3.文档查询(find()findOne()
    • 1.插入文档(需要指定集合collectionName,然后,插入文档)

      • db.collectionName.insert({})
        在这里插入图片描述

        1.文档中的数据结构为json格式
        2.每条数据中字段可能不一致
        3.每天数中想同的字段值类型也可能不一致

    • 2.查看文档(需要指定集合collectionName,然后,查看文档内容)

      • db.collectionName.find();
        在这里插入图片描述
    • 3.查询当前集合根据key去重后的值列表

      • db.collectionName.distinct('key');
        在这里插入图片描述
    • 4.查询相等

      • db.collectionName.find({"name": "lucy"});
        在这里插入图片描述
    • 4.查询大于

      • db.collectionName.find({"key": {$gt: number}});
        在这里插入图片描述
    • 5.查询小于

      • db.collectionName.find({"key": {$lt: number}});
    • 6.查询大于等于

      • db.collectionName.find({"key": {$gte: number}});
    • 7.查询小于等于

      • db.collectionName.find({"key": {$lte: number}});
    • 8.查询大于等于并且小于等于

      • db.collectionName.find({"key": {$gte: number, $lte: number}});
    • 9.查询大于等于小于等于

      • db.collectionName.find({$or: [{"key": {$gte: number}}, {"key": {$lte: number}}]});
    • 10.查询包含(模糊查询

      • db.collectionName.find({"key": /value/});
        在这里插入图片描述
    • 10.查询以什么开头

      • db.collectionName.find({"key": /^value/});
    • 11.查询以什么结尾

      • db.collectionName.find({"key": /value$/});
    • 12.查询指定列

      • db.collectionName.find({}, {"key":1});
        在这里插入图片描述
        在这里插入图片描述
    • 13.排序(1:升序,-1:降序)

      • db.collectionName.find().sort({"key": 1});
        在这里插入图片描述
    • 14.查询前1条数据

      • db.collectionName.find().limit(1);
        在这里插入图片描述
    • 15.跳过前1条数据,查询前一条数据(可以用作分页)

      • db.collectionName.find().skip(1);
        在这里插入图片描述
    • 16.统计数量

      • db.collectionName.find().count();
    3.文档修改(update()
    • 1.更新原有数据中的字段值$set
      • db.collectionName.update({"key": "value"}, {$set: {"key": "value"}})
        在这里插入图片描述
    • 2.替换原有数据(高版本会报错,测试6.0)
      • db.collectionName.update({"key": "value"}, {"key": "value"})
    • 3.在原有数据基础上增加字段
      • db.collectionName.update({"key": "value"}, {$set: {"newKey": "value"}});
        在这里插入图片描述
    • 4.更新多条数据
      • db.collectionName.update({"key": "value"}, {$set: {"newKey": "value"}}, {"multi": true});
    • 5.对原有数据进行加法运算$inc
      • db.collectionName.update({"key": "value"}, {$inc: {"key": "value"}}, false, true);
        在这里插入图片描述
    3.文档删除(remove()
    • 1.删除匹配到的所有数据
      • db.collectionName.remove({"key": "value"});
    • 2.删除匹配到的一条数据
      • db.collectionName.remove({"key": "value"}, {justOne: true});
        在这里插入图片描述

    3.Mongodb索引

    1.定义
    • 索引是对数据库表中一列或多列值进行排序的一种结构,可以让我们查询数据库变得更快。
    2.创建
    • db.collectionName.ensureIndex({"key": 1});
    3.获取
    • db.collectionName.getIndexes();
    4.删除
    • db.collectionName.dropIndex({"name": 1});
    5.设置索引名称
    • db.collectionName.ensureIndex({"key": 1}, {"name": "selfIndexName"});
    6.复合索引
    • 符合索引遵循最左侧原则,即查询字段从最左侧匹配,如果匹配到,则可以命中索引,否则不会命中索引
    • 例如:db.collectionName.ensureIndex({"name": 1, "age": 1});

      db.collectionName.find({"name": "xxx", "age": xx});,可以命中索引
      db.collectionName.find({"age": xx, "name": "xxx"});,可以命中索引
      db.collectionName.find({"name": "xxx", "otherKey": "xxx"});,可以命中索引
      db.collectionName.find({"name": "xxx"});,可以命中索引
      db.collectionName.find({"age": xx});,不会命中索引

    7.唯一索引
    • 可以防止字段值重复
      • db.collectionName.ensureIndex({"key": 1}, {"unique": true});
    8.注意
    • 随者集合的增长,需要针对查询中大量的排序做索引,如果没有对排序的key设置索引,MongoDB在将所有数据提取到内存并排序时,会导致内存爆满,从而查询失败。

    4.查看查询语句执行时间

    • db.user.find().explain("executionStats");
    • 耗时:executionTimeMillisEstimate,单位:毫秒
    • 命中索引:indexBounds:
  • 相关阅读:
    js函数( 普通函数、箭头函数 ) 内部this的指向
    实验八—基本统计分析(一)
    【多线程】线程管理
    3D可视化工厂是如何实现的?
    用python找出400多万次KDJ金叉死叉,胜率有多高?附代码
    力扣94二叉树的中序遍历
    非root用户,没有root权限,安装nginx
    k8s驱逐篇(7)-kube-controller-manager驱逐-taintManager源码分析
    学习open62541 --- [69] Client监测多个变量值
    实时云渲染应用之虚拟仿真项目的四大优势
  • 原文地址:https://blog.csdn.net/qq_42517220/article/details/126711972