(1) 设置键值:
set key1 redis
(2) 读取键值:
get key1
(3) 数值类型自增1:
incr key1
(4) 数值类型自减1:
decr key1
(5) 查看值的长度:
STRLEN key1
(1)对列表city插入元素:Shanghai Suzhou Hangzhou
LPUSH city Shanghai Suzhou Hangzhou
(2)将列表city里的头部的元素移除
LPOP city
(3)将name列表的尾部元素移除到number列表的头部
RPOPLPUSH name number
(4) 对一个已存在的列表插入新元素
RPUSH name e
(5)查看list的值长度
LLEN name
(1)设置一个hash表,order表里包括的键值信息有:id:1,customer_name:张三
HMSET order id 1 customer_name "张三"
(2) 创建一个hash表,表里的键值批量插入
HMSET order1 name n add a ip i
(3) 获取order对应的map的所有key
HKEYS order
(4) 获取order对应的map的键值数量
HLEN order
(5) 获取order表里的id值
HGET order id
(1) 查看key是否存在
EXISTS key1
(2) 查找满足pattern的keys
KEYS or*
(3) 查看key的超时时间
TTL key1
(4) 遍历key
KEYS *
use grade
db.createCollection("class")
db.class.insert([
{name:"小计",age:10,sex:'m',hobby:['draw','basketball','pingpong']},
{name:"小红",age:8,sex:'f',hobby:['draw','dance','pingpong']},
{name:"小明",age:11,sex:'m',hobby:['football','dance','sing']},
{name:"小张",age:13,sex:'f',hobby:['draw','dance','pingpong']},
{name:"小龙",age:6,sex:'f',hobby:['draw','football','sing']},
{name:"小李",age:7,sex:'m',hobby:['draw','dance','pingpong']},
{name:"小陈",age:5,sex:'f',hobby:['draw','dance','sing']},
{name:"小钱",age:3,sex:'m',hobby:['pingpong','dance','sing']},
{name:"小明",age:6,sex:'f',hobby:['draw','dance','pingpong']},
{name:"小米",age:12,sex:'m',hobby:['draw','dance','football']},
{name:"小小",age:14,sex:'f',hobby:['draw','basketball','football']},
{name:"小康",age:15,sex:'m',hobby:['draw','basketball','pingpong']}
])
查看班级所有人信息
db.class.find()
查看班级中年龄为8岁的学生信息
db.class.find({age:8})
查看年龄大于10岁的学生信息
db.class.find({age:8})
db.class.find({age:{$gte:4,$lte:8}})
db.class.find({age:6,sex:"m"})
找到年龄小于7岁或者大于10岁的学生
db.class.find({$or:[{age:{$lte:7}},{age:{$gte:10}}]})
找到年龄是8岁或者11岁的学生
db.class.find({age:{$in:[7,11]}})
找到兴趣爱好有两项的学生
db.class.find({'hobby':{$size:2}})
找到兴趣爱好有draw的学生
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)
删除所有 年级大于12或者小于4岁的学生
db.class.remove({$or:[{age:{$lt:4}},{age:{$gt:12}}]})
将小红的年龄变为8岁 兴趣爱好变为 跳舞 画画
db.class.update({'name':'小红'},{$set:{'age':8,'hobby':['dance','draw',]}})
追加小明兴趣爱好 打篮球
db.class.update({'name':'小明'},{$push:{'hobby':'basketball'}})
小李兴趣爱好增加 吹牛 打篮球
db.class.update({'name':'小李'},{$pushAll:{'hobby':['basketball','computer']}})
小李增加爱好,跑步和唱歌,但是不要和以前的重复
db.class.update({'name':'小李'},{$addToSet:{'hobby':{$each:['run','sing']}}})
该班所有同学年龄加1
db.class.update({},{$inc:{age:1}},false,true)
删除小明的sex属性
db.class.update({name:'小明'},{$unset:{sex:0}})
删除小李兴趣中的第一项
db.class.update({name:'小李'},{$pop:{hobby:-1}})
将小红兴趣中的画画爱好删除
db.class.update({name:'小红'},{$pull:{hobby:'draw'}})
按照性别分组统计每组人数
db.class.aggregate({$group:{_id:'$sex',num:{$sum:1}}})
按照姓名分组,过滤出有重名的同学
db.class.aggregate([{$group:{_id:'$name',num:{$sum:1}}},{$match:{num:{$gt:1}}}])
统计每名男生的语文成绩
db.class.aggregate([{$match:{sex:'m'}},{$project:{_id:0,name:1,'score.chinese':1}}])
将女生按照英语分数降序排列
db.class.aggregate([{$match:{sex:'f'}},{$sort:{'score.english':-1}}])