目录
一、redis
1、 string类型数据的命令操作
2、 list类型数据的命令操作
3、 hash类型数据的命令操作
4、Keys相关的命令操作
二、mongodb
1. 创建一个数据库 名字grade
2. 数据库中创建一个集合名字 class
3. 集合中插入若干数据 文档格式如下
4. 查找
5. 增加、更新、删除、统计
增加分数域
(1) 设置键值:
(2) 读取键值:
(3) 数值类型自增1:
(4) 数值类型自减1:
(5) 查看值的长度:
(1)对列表city插入元素:Shanghai Suzhou Hangzhou
(2)将列表city里的头部的元素移除
(3)将name列表的尾部元素移除到number列表的头部
(4) 对一个已存在的列表插入新元素
(5)查看list的值长度
(1) 设置一个hash表,order表里包括的键值信息有:id:1,customer_name:张三
(2) 创建一个hash表,表里的键值批量插入
(3) 获取order对应的map的所有key
(4) 获取order对应的map的键值数量
(5) 获取order表里的id值
(1) 查看key是否存在
(2) 查找满足pattern的keys
(3) 查看key的超时时间
(4) 遍历key
use grade
db.createCollection("class")
{name:'zhang',age;10,sex:'m',hobby:['a','b','c']}
hobby: draw sing dance basketball football pingpong computer
db.class.insert([{"name":"zhangsan","age":10,"sex":"m","hobby":["draw","sing"]},{"name":"lisi","age":12,"sex":"m","hobby":["dance","computer"]},{"name":"wangwu","age":11,"sex":"m","hobby":["draw","football"]},{"name":"zhaofang","age":11,"sex":"f","hobby":["draw","sing","dance"]},{"name":"lily","age":10,"sex":"f","hobby":["sing","dance"]},{"name":"haha","age":8,"sex":"m","hobby":["pingpong","football"]},{"name":"grace","age":7,"sex":"f","hobby":["draw","football","computer"]},{"name":"bob","age":13,"sex":"m","hobby":["football","computer"]}])
查看班级所有人信息
db.class.find()
db.class.find({age:8})
db.class.find({age:{$gt:10}})
db.class.find({age:{$gte:4,$lte:8}}).pretty()
db.class.find({age:6,sex:'m'})
找到年龄小于7岁或者大于10岁的学生
db.class.find({$or:[{age:{$lt:7}},{age:{$gt:10}}]})
db.class.find({age:{$in:[8,11]}})
db.class.find({hobby:{$size:2}})
db.class.find({hobby:"draw"})
db.class.find({hobby:{$all:["draw","dance"]}})
db.class.find({hobby:{$size:3}}).count()
db.class.find().sort({age:-1}).skip(1).limit(1)
db.class.distinct('hobby')
db.class.find().sort({age:-1}).limit(3)
db.class.remove({$or:[{age:{$gt:12}},{age:{$lt:4}}]})
db.class.updateOne({name:"小红"},{$set:{age:8, hobby:["draw","dance"]}})
db.class.updateOne({name:"小明"},{$push:{hobby:'sing'}})
db.class.updateOne({name:"小明"},{$push:{hobby:["吹牛","basketball"]}})
db.class.updateOne({name:"小明"},{$addToSet:{hobby:{$each:["run","sing"]}}})
db.class.updateMany({},{$inc:{age:1}})
db.class.updateOne({},{$unset:{sex:""}})
db.class.update({name:"小李"},{'$pop':{hobby:-1}})
db.class.update({name:"小红"},{$pull:{hobby:"draw"}})
score:{'chinese':88,'english':78,'math':98}
db.class.aggregate({$group:{_id:'$sex',num:{$sum:1}}})
2. 按照姓名分组,过滤出有重名的同学
db.class.aggregate([{$group:{_id:'$name',num:{$sum:1}}},{$match:{num:{$gt:1}}}])
3. 统计每名男生的语文成绩
db.class.aggregate([{$match:{sex:"男"}},{$project:{_id:0,name:1,'score.chinese':1}}])
4. 将女生按照英语分数降序排列
db.class3.aggregate([{$match:{sex:"女"}},{$sort:{'score.english':-1}}])