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: