MySQL-Redis数据类型操作和MongoDB基本操作

Redis

1、 string类型数据的命令操作:

(1) 设置键值:

127.0.0.1:6379> set key1 1

(2) 读取键值:

127.0.0.1:6379> get key1
"1"

(3) 数值类型自增1:

127.0.0.1:6379> incr key1
(integer) 2

(4) 数值类型自减1:

127.0.0.1:6379> decr key1
(integer) 1

(5) 查看值的长度:

127.0.0.1:6379> strlen key1
(integer) 1

2、 list类型数据的命令操作:

(1)对列表city插入元素:Shanghai Suzhou Hangzhou

127.0.0.1:6379> lpush city Shanghai Suzhou Hangzhou
(integer) 3
127.0.0.1:6379> lrange city 0 -1
1) "Hangzhou"
2) "Suzhou"
3) "Shanghai"

(2)将列表city里的头部的元素移除

127.0.0.1:6379> lpop city 
"Hangzhou"
127.0.0.1:6379> lrange city 0 -1
1) "Suzhou"
2) "Shanghai"

(3)将name列表的尾部元素移除到number列表的头部

#操作前
127.0.0.1:6379> lrange name 0 -1
1) "lisi"
2) "zhangsan"
127.0.0.1:6379> lrange number 0 -1
1) "zhangwuji"
2) "wangwu"
#将name最后一个数据移到number中
127.0.0.1:6379> rpoplpush name number
"zhangsan"
#操作后
127.0.0.1:6379> lrange number 0 -1
1) "zhangsan"
2) "zhangwuji"
3) "wangwu"
127.0.0.1:6379> lrange name 0 -1
1) "lisi"

(4) 对一个已存在的列表插入新元素

127.0.0.1:6379> rpushx name zhangfei
(integer) 2
127.0.0.1:6379> lrange name 0 -1
1) "lisi"
2) "zhangfei"

(5)查看list的值长度

127.0.0.1:6379> llen list
(integer) 0
127.0.0.1:6379> llen name
(integer) 2

3、 hash类型数据的命令操作:

(1) 设置一个hash表,order表里包括的键值信息有:id:1,customer_name:张三

127.0.0.1:6379> hset order id 1 customer_name '张三'
(integer) 2
[root@localhost ~]# redis-cli --raw
127.0.0.1:6379> hgetall order
id
1
customer_name
张三

(2) 创建一个hash表,表里的键值批量插入

127.0.0.1:6379> hmset key2 id 1 name zhangsan age 24
OK
127.0.0.1:6379> hgetall key2
id
1
name
zhangsan
age
24

(3) 获取order对应的map的所有key

127.0.0.1:6379> hkeys order
id
customer_name

(4) 获取order对应的map的键值数量

127.0.0.1:6379> hlen order
2

(5) 获取order表里的id值

127.0.0.1:6379> hmget order id
1

4、Keys相关的命令操作

(1) 查看key是否存在

#存在返回1,不存在返回0
127.0.0.1:6379> exists key1
1

(2) 查找满足pattern的keys

127.0.0.1:6379> keys key*
key1
key2

(3) 查看key的超时时间

127.0.0.1:6379> expire key1 20
1
127.0.0.1:6379> ttl key1
-2
#如果键存在且具有超时时间,则返回剩余时间。
#如果键不存在或者没有设置超时时间(永久有效),则返回 -1。
#如果键存在但已过期(超时时间为 0),则返回 -2。

(4) 遍历key

127.0.0.1:6379> keys *
order
key2

MongoDB

1. 创建一个数据库 名字grade

> use grade
switched to db grade

2. 数据库中创建一个集合名字 class

3. 集合中插入若干数据 文档格式如下

{name:‘zhang’,age;10,sex:‘m’,hobby:[‘a’,‘b’,‘c’]}
hobby: draw sing dance basketball football pingpong
computer

db.class.insertMany([
  { name: "小红", age: 10, sex: 'f', hobby: ['draw', 'sing', 'basketball'] },
  { name: "小明", age: 10, sex: 'm', hobby: ['dance', 'basketball', 'football'] },
  { name: "小王", age:11 , sex: 'm', hobby: ['draw', 'sing', 'dance'] },
  { name: "小李", age: 7, sex: 'm', hobby: ['computer', 'sing', 'pingpong'] },
  { name: "小赵", age: 4, sex: 'f', hobby: ['draw', 'football', 'dance'] },
  { name: "小鬼", age: 6, sex: 'm', hobby: ['sing', 'computer'] },
  { name: "小猪", age: 8, sex: 'm', hobby: ['pingpong'] },
  { name: "小杨", age: 9, sex: 'f', hobby: ['draw', 'pingpong', 'football'] },
  { name: "小天", age: 7, sex: 'm', hobby: ['computer','dance'] },
  { name: "小楚", age: 9, sex: 'f', hobby: ['sing', 'pingpong'] },
  { name: "小吴", age: 12, sex: 'm', hobby: ['sing', 'computer'] },
  { name: "小豪", age: 11, sex: 'm', hobby: ['football'] }
])

4. 查找练习

查看班级所有人信息

> db.class.find({},{_id:0})
{ "name" : "小红", "age" : 10, "sex" : "f", "hobby" : [ "draw", "sing", "basketball" ] }
{ "name" : "小明", "age" : 10, "sex" : "m", "hobby" : [ "dance", "basketball", "football" ] }
{ "name" : "小王", "age" : 11, "sex" : "m", "hobby" : [ "draw", "sing", "dance" ] }
{ "name" : "小李", "age" : 7, "sex" : "m", "hobby" : [ "computer", "sing", "pingpong" ] }
{ "name" : "小赵", "age" : 4, "sex" : "f", "hobby" : [ "draw", "football", "dance" ] }
{ "name" : "小鬼", "age" : 6, "sex" : "m", "hobby" : [ "sing", "computer" ] }
{ "name" : "小猪", "age" : 8, "sex" : "m", "hobby" : [ "pingpong" ] }
{ "name" : "小杨", "age" : 9, "sex" : "f", "hobby" : [ "draw", "pingpong", "football" ] }
{ "name" : "小天", "age" : 7, "sex" : "m", "hobby" : [ "computer", "dance" ] }
{ "name" : "小楚", "age" : 9, "sex" : "f", "hobby" : [ "sing", "pingpong" ] }
{ "name" : "小吴", "age" : 12, "sex" : "m", "hobby" : [ "sing", "computer" ] }
{ "name" : "小豪", "age" : 11, "sex" : "m", "hobby" : [ "football" ] }

查看班级中年龄为8岁的学生信息

> db.class.find({age:8},{_id:0})
{ "name" : "小猪", "age" : 8, "sex" : "m", "hobby" : [ "pingpong" ] }

查看年龄大于10岁的学生信息

> db.class.find({age:{$gt:10}},{_id:0})
{ "name" : "小王", "age" : 11, "sex" : "m", "hobby" : [ "draw", "sing", "dance" ] }
{ "name" : "小吴", "age" : 12, "sex" : "m", "hobby" : [ "sing", "computer" ] }
{ "name" : "小豪", "age" : 11, "sex" : "m", "hobby" : [ "football" ] }

查看年龄在 4—8岁之间的学生信息

> db.class.find({age:{$gte:4,$lte:8}},{_id:0})
{ "name" : "小李", "age" : 7, "sex" : "m", "hobby" : [ "computer", "sing", "pingpong" ] }
{ "name" : "小赵", "age" : 4, "sex" : "f", "hobby" : [ "draw", "football", "dance" ] }
{ "name" : "小鬼", "age" : 6, "sex" : "m", "hobby" : [ "sing", "computer" ] }
{ "name" : "小猪", "age" : 8, "sex" : "m", "hobby" : [ "pingpong" ] }
{ "name" : "小天", "age" : 7, "sex" : "m", "hobby" : [ "computer", "dance" ] }

找到年龄为6岁且为男生的学生

> db.class.find({age:6,sex:'m'},{_id:0})
{ "name" : "小鬼", "age" : 6, "sex" : "m", "hobby" : [ "sing", "computer" ] }

找到年龄小于7岁或者大于10岁的学生

> db.class.find({$or:[{age:{$lt:7}},{age:{$gt:10}}]},{_id:0})
{ "name" : "小王", "age" : 11, "sex" : "m", "hobby" : [ "draw", "sing", "dance" ] }
{ "name" : "小赵", "age" : 4, "sex" : "f", "hobby" : [ "draw", "football", "dance" ] }
{ "name" : "小鬼", "age" : 6, "sex" : "m", "hobby" : [ "sing", "computer" ] }
{ "name" : "小吴", "age" : 12, "sex" : "m", "hobby" : [ "sing", "computer" ] }
{ "name" : "小豪", "age" : 11, "sex" : "m", "hobby" : [ "football" ] }

找到年龄是8岁或者11岁的学生

> db.class.find({$or:[{age:8},{age:11}]},{_id:0})
{ "name" : "小王", "age" : 11, "sex" : "m", "hobby" : [ "draw", "sing", "dance" ] }
{ "name" : "小猪", "age" : 8, "sex" : "m", "hobby" : [ "pingpong" ] }
{ "name" : "小豪", "age" : 11, "sex" : "m", "hobby" : [ "football" ] }

找到兴趣爱好有两项的学生

> db.class.find({ $where: "this.hobby.length == 2" }, { _id: 0 })
{ "name" : "小鬼", "age" : 6, "sex" : "m", "hobby" : [ "sing", "computer" ] }
{ "name" : "小天", "age" : 7, "sex" : "m", "hobby" : [ "computer", "dance" ] }
{ "name" : "小楚", "age" : 9, "sex" : "f", "hobby" : [ "sing", "pingpong" ] }
{ "name" : "小吴", "age" : 12, "sex" : "m", "hobby" : [ "sing", "computer" ] }

找到兴趣爱好有draw的学生

> db.class.find({ hobby:'draw' }, { _id: 0 })
{ "name" : "小红", "age" : 10, "sex" : "f", "hobby" : [ "draw", "sing", "basketball" ] }
{ "name" : "小王", "age" : 11, "sex" : "m", "hobby" : [ "draw", "sing", "dance" ] }
{ "name" : "小赵", "age" : 4, "sex" : "f", "hobby" : [ "draw", "football", "dance" ] }
{ "name" : "小杨", "age" : 9, "sex" : "f", "hobby" : [ "draw", "pingpong", "football" ] }

找到既喜欢画画又喜欢跳舞的学生

> db.class.find({hobby:{$all:['draw','dance']}}, { _id: 0 })
{ "name" : "小王", "age" : 11, "sex" : "m", "hobby" : [ "draw", "sing", "dance" ] }
{ "name" : "小赵", "age" : 4, "sex" : "f", "hobby" : [ "draw", "football", "dance" ] }

统计爱好有三项的学生人数

> db.class.find({ $where: "this.hobby.length == 3" }, { _id: 0 })
{ "name" : "小红", "age" : 10, "sex" : "f", "hobby" : [ "draw", "sing", "basketball" ] }
{ "name" : "小明", "age" : 10, "sex" : "m", "hobby" : [ "dance", "basketball", "football" ] }
{ "name" : "小王", "age" : 11, "sex" : "m", "hobby" : [ "draw", "sing", "dance" ] }
{ "name" : "小李", "age" : 7, "sex" : "m", "hobby" : [ "computer", "sing", "pingpong" ] }
{ "name" : "小赵", "age" : 4, "sex" : "f", "hobby" : [ "draw", "football", "dance" ] }
{ "name" : "小杨", "age" : 9, "sex" : "f", "hobby" : [ "draw", "pingpong", "football" ] }

找出本班年龄第二大的学生

> db.class.find({},{_id:0}).sort({age:-1}).skip(1).limit(1)
{ "name" : "小王", "age" : 11, "sex" : "m", "hobby" : [ "draw", "sing", "dance" ] }

查看学生的兴趣范围

> db.class.distinct('hobby')
[
	"basketball",
	"computer",
	"dance",
	"draw",
	"football",
	"pingpong",
	"sing"
]

将学生按年龄排序找到年龄最大的三个

> db.class.find({},{_id:0}).sort({age:-1}).limit(3)
{ "name" : "小吴", "age" : 12, "sex" : "m", "hobby" : [ "sing", "computer" ] }
{ "name" : "小王", "age" : 11, "sex" : "m", "hobby" : [ "draw", "sing", "dance" ] }
{ "name" : "小豪", "age" : 11, "sex" : "m", "hobby" : [ "football" ] }

删除所有 年纪大于11或者小于5岁的学生

> db.class.deleteMany({$or:[{age:{$gt:11}},{age:{$lt:5}}]})
{ "acknowledged" : true, "deletedCount" : 2 }
> db.class.find()
{ "_id" : ObjectId("64b27b17424df594907ba6b1"), "name" : "小红", "age" : 10, "sex" : "f", "hobby" : [ "draw", "sing", "basketball" ] }
{ "_id" : ObjectId("64b27b17424df594907ba6b2"), "name" : "小明", "age" : 10, "sex" : "m", "hobby" : [ "dance", "basketball", "football" ] }
{ "_id" : ObjectId("64b27b17424df594907ba6b3"), "name" : "小王", "age" : 11, "sex" : "m", "hobby" : [ "draw", "sing", "dance" ] }
{ "_id" : ObjectId("64b27b17424df594907ba6b4"), "name" : "小李", "age" : 7, "sex" : "m", "hobby" : [ "computer", "sing", "pingpong" ] }
{ "_id" : ObjectId("64b27b17424df594907ba6b6"), "name" : "小鬼", "age" : 6, "sex" : "m", "hobby" : [ "sing", "computer" ] }
{ "_id" : ObjectId("64b27b17424df594907ba6b7"), "name" : "小猪", "age" : 8, "sex" : "m", "hobby" : [ "pingpong" ] }
{ "_id" : ObjectId("64b27b17424df594907ba6b8"), "name" : "小杨", "age" : 9, "sex" : "f", "hobby" : [ "draw", "pingpong", "football" ] }
{ "_id" : ObjectId("64b27b17424df594907ba6b9"), "name" : "小天", "age" : 7, "sex" : "m", "hobby" : [ "computer", "dance" ] }
{ "_id" : ObjectId("64b27b17424df594907ba6ba"), "name" : "小楚", "age" : 9, "sex" : "f", "hobby" : [ "sing", "pingpong" ] }
{ "_id" : ObjectId("64b27b17424df594907ba6bc"), "name" : "小豪", "age" : 11, "sex" : "m", "hobby" : [ "football" ] }

增加、更新、删除作业 :

使用之前的grade数据库

  1. 将小红的年龄变为8岁 兴趣爱好变为 跳舞 画画
> db.class.update({'name':'小红'},{$set:{'age':8,'hobby':['dance','draw']}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
  1. 追加小明兴趣爱好 唱歌
> db.class.update({'name':'小明'},{$push:{'hobby':'sing'}})
  1. 小王兴趣爱好增加 吹牛 打篮球
> db.class.update({'name':'小王'},{$push:{'hobby':{$each:['basketball','吹牛']}}})
#或者,但$pushall在 MongoDB 4.2 版本后已被废弃
> db.class.update({'name':'小王'},{$pushAll:{'hobby':['basketball','吹牛']}})
  1. 小李增加爱好,跑步和唱歌,但是不要和以前的重复
> db.class.update({'name':'小李'},{$addToSet:{hobby:{$each:['running','sing']}}})
  1. 该班所有同学年龄加1
> db.class.update({},{$inc:{age:1}},false,true)
  1. 删除小明的sex属性
> db.class.update({'name':'小明'},{$unset:{sex:0}})
  1. 删除小李兴趣中的第一项
> db.class.update({'name':'小李'},{$pop:{hobby:-1}})
  1. 将小红兴趣中的画画爱好删除
> db.class.update({'name':'小红'},{$pull:{hobby:'draw'}})

使用之前的grade数据库
增加分数域 score:{‘chinese’:88,‘english’:78,‘math’:98}

db.class.update({ name: "小红" }, { $set: { score: { chinese: 88, english: 78, math: 98 } } })
db.class.update({ name: "小明" }, { $set: { score: { chinese: 92, english: 85, math: 96 } } })
db.class.update({ name: "小王" }, { $set: { score: { chinese: 80, english: 90, math: 95 } } })
db.class.update({ name: "小李" }, { $set: { score: { chinese: 75, english: 80, math: 82 } } })
db.class.update({ name: "小赵" }, { $set: { score: { chinese: 85, english: 70, math: 89 } } })
db.class.update({ name: "小鬼" }, { $set: { score: { chinese: 78, english: 82, math: 85 } } })
db.class.update({ name: "小猪" }, { $set: { score: { chinese: 92, english: 85, math: 76 } } })
db.class.update({ name: "小杨" }, { $set: { score: { chinese: 89, english: 92, math: 78 } } })
db.class.update({ name: "小天" }, { $set: { score: { chinese: 86, english: 80, math: 90 } } })
db.class.update({ name: "小楚" }, { $set: { score: { chinese: 93, english: 88, math: 94 } } })
db.class.update({ name: "小吴" }, { $set: { score: { chinese: 95, english: 90, math: 85 } } })
db.class.update({ name: "小豪" }, { $set: { score: { chinese: 80, english: 85, math: 90 } } })
  1. 按照性别分组统计每组人数
> db.class.aggregate([ { $group: { _id: "$sex", count: { $sum: 1 } } } ])
{ "_id" : "f", "count" : 3 }
{ "_id" : null, "count" : 1 }
{ "_id" : "m", "count" : 6 }
  1. 按照姓名分组,过滤出有重名的同学
#可以去更改一个重名的测试
> db.class.aggregate([{$group:{_id:'$name',num:{$sum:1}}},{$match:{num:{$gt:1}}}])
  1. 统计每名男生的语文成绩
> db.class.aggregate([{$match:{sex:'m'}},{$project:{_id:0,name:1,'score.chinese':1}}])
{ "name" : "小王", "score" : { "chinese" : 80 } }
{ "name" : "小李", "score" : { "chinese" : 75 } }
{ "name" : "小鬼", "score" : { "chinese" : 78 } }
{ "name" : "小猪", "score" : { "chinese" : 92 } }
{ "name" : "小天", "score" : { "chinese" : 86 } }
{ "name" : "小豪", "score" : { "chinese" : 80 } }
  1. 将女生按照英语分数降序排列
> db.class.aggregate([{$match:{sex:'f'}},{$sort:{'score.english':-1}}])
{"name" : "小杨", "age" : 10, "sex" : "f", "hobby" : [ "draw", "pingpong", "football" ], "score" : { "chinese" : 89, "english" : 92, "math" : 78 } }
{"name" : "小楚", "age" : 10, "sex" : "f", "hobby" : [ "sing", "pingpong" ], "score" : { "chinese" : 93, "english" : 88, "math" : 94 } }
{"name" : "小红", "age" : 9, "sex" : "f", "hobby" : [ "dance" ], "score" : { "chinese" : 88, "english" : 78, "math" : 98 } }

你可能感兴趣的:(mysql,redis,mongodb)