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:
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值