Redis,MongoDB基本操作练习题

文章目录

语法不会可以在官网上查询MongoDB教程

NoSQL练习:

Redis

String类型基本操作:

bash 复制代码
#set
192.168.110.128:6379> set name 'harry'
OK
#get
192.168.110.128:6379> get name
"harry"

#数字自增
192.168.110.128:6379> set num 2
OK
192.168.110.128:6379> get num
"2"
192.168.110.128:6379> INCR num
(integer) 3
192.168.110.128:6379> get num
"3"
#自减
192.168.110.128:6379> DECR num
(integer) 2
192.168.110.128:6379> get num
"2"

#获取字符长度
192.168.110.128:6379> STRLEN name
(integer) 5
192.168.110.128:6379> get name
"harry"

List类型基本操作:

bash 复制代码
#对列表city插入元素:Shanghai Suzhou Hangzhou
192.168.110.128:6379> LPUSH city shanghai
(integer) 1
192.168.110.128:6379> LPUSH city suzhou
(integer) 2
192.168.110.128:6379> LPUSH city hangzhou
(integer) 3
#将列表city里的头部的元素移除
192.168.110.128:6379> LPOP city
"hangzhou"
#将name列表的尾部元素移除到number列表的头部
192.168.110.128:6379> RPOPLPUSH city number
"beijing"
192.168.110.128:6379> LINDEX number 0
"beijing"
#对一个已存在的列表插入新元素
192.168.110.128:6379> RPUSH  city beijing
(integer) 4
192.168.110.128:6379> LINDEX city -1
"beijing"
#查看list的值长度
192.168.110.128:6379> LLEN city
(integer) 3

hash类型基本操作:

bash 复制代码
#设置一个hash表,order表里包括的键值信息有:id:1,customer_name:张三
192.168.110.128:6379> HSET hp id 1 
OK
192.168.110.128:6379> HGET hp
1) "id"
2) "1"
#创建一个hash表,表里的键值批量插入
192.168.110.128:6379> HMSET hp2 id 1 name 'lisi' id 2 name 'wuwang'
OK
192.168.110.128:6379> HGETALL hp2
1) "id"
2) "2"
3) "name"
4) "wuwang"
#获取order对应的map的所有key
192.168.110.128:6379> HKEYS order
1) "id"
2) "name"
#获取order对应的map的键值数量
192.168.110.128:6379> HLEN order
(integer) 2
#获取order表里的id值
192.168.110.128:6379> HVALS order
1) "1"
2) "bob"
#查看key是否存在
192.168.110.128:6379> EXISTS name
(integer) 1
#查找满足pattern的keys
192.168.110.128:6379> KEYS name
1) "name"
#查看key的超时时间
192.168.110.128:6379> TTL name
(integer) -1
#遍历key
192.168.110.128:6379> KEYS *
1) "k2"
2) "name"
3) "hp"
4) "number"
5) "hp2"
6) "order"
7) "city"
8) "k1"
9) "num"

MongoDB:

创建一个数据库 名字grade:

bash 复制代码
> use grede
switched to db grede
#有则使用没有则创建

创建class集合:

bash 复制代码
> db.createCollection("class")

集合中插入若干数据:

bash 复制代码
文档格式如下
{name:'zhang',age;10,sex:'m',hobby:['a','b','c']}
hobby: draw  sing  dance  basketball football  pingpong  computer

> db.class.insertMany([{name:'lisi',age:4,sex:'m',hobby:['dance','sing','draw']},{name:'wangwu',age:7,sex:'f',hooby:['sing','dance','computer']},{name:'zhangsan',age:6,sex:'m',hobby:['game','code','dance']}])
{
        "acknowledged" : true,
        "insertedIds" : [
                ObjectId("64b28a4ffb91465cc9ea745c"),
                ObjectId("64b28a4ffb91465cc9ea745d"),
                ObjectId("64b28a4ffb91465cc9ea745e")
        ]
}

查看班级所有人信息:

bash 复制代码
> db.class.find()
{ "_id" : ObjectId("64b28a4ffb91465cc9ea745c"), "name" : "lisi", "age" : 4, "sex" : "m", "hobby" : [ "dance", "sing", "draw" ] }
{ "_id" : ObjectId("64b28a4ffb91465cc9ea745d"), "name" : "wangwu", "age" : 7, "sex" : "f", "hooby" : [ "sing", "dance", "computer" ] }
{ "_id" : ObjectId("64b28a4ffb91465cc9ea745e"), "name" : "zhangsan", "age" : 6, "sex" : "m", "hobby" : [ "game", "code", "dance" ] }

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

bash 复制代码
> db.class.find({age:8})
{ "_id" : ObjectId("64b28b7efb91465cc9ea745f"), "name" : "sunqi", "age" : 8, "sex" : "m", "hobby" : [ "dance", "computer", "draw" ] }

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

bash 复制代码
> db.class.find({age:{$gt:10}})

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

bash 复制代码
> db.class.find({age:{$gt:4,$lt:8}})
{ "_id" : ObjectId("64b28a4ffb91465cc9ea745d"), "name" : "wangwu", "age" : 7, "sex" : "f", "hooby" : [ "sing", "dance", "computer" ] }
{ "_id" : ObjectId("64b28a4ffb91465cc9ea745e"), "name" : "zhangsan", "age" : 6, "sex" : "m", "hobby" : [ "game", "code", "dance" ] }

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

bash 复制代码
> db.class.find({age:6,sex:'m'})
{ "_id" : ObjectId("64b28a4ffb91465cc9ea745e"), "name" : "zhangsan", "age" : 6, "sex" : "m", "hobby" : [ "game", "code", "dance" ] }

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

bash 复制代码
> db.class.find({$or: [{age:{$lt:7}},{age:{$gt:10}}]})
{ "_id" : ObjectId("64b28a4ffb91465cc9ea745c"), "name" : "lisi", "age" : 4, "sex" : "m", "hobby" : [ "dance", "sing", "draw" ] }
{ "_id" : ObjectId("64b28a4ffb91465cc9ea745e"), "name" : "zhangsan", "age" : 6, "sex" : "m", "hobby" : [ "game", "code", "dance" ] }

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

bash 复制代码
> db.class.find({$or: [{age:8},{age:11}]})
{ "_id" : ObjectId("64b28b7efb91465cc9ea745f"), "name" : "sunqi", "age" : 8, "sex" : "m", "hobby" : [ "dance", "computer", "draw" ] }

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

bash 复制代码
db.class.find({hobby:{$size:2}})
{ "_id" : ObjectId("64b4b267324c94c22a9d0010"), "name" : "小李", "age" : 4, "sex" : "m", "hobby" : [ "dance", "sing" ] }

找到兴趣爱好有draw的学生:

bash 复制代码
> db.class.find({hobby:/^draw/})
{ "_id" : ObjectId("64b28a4ffb91465cc9ea745c"), "name" : "lisi", "age" : 4, "sex" : "m", "hobby" : [ "dance", "sing", "draw" ] }
{ "_id" : ObjectId("64b28b7efb91465cc9ea745f"), "name" : "sunqi", "age" : 8, "sex" : "m", "hobby" : [ "dance", "computer", "draw" ] }

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

bash 复制代码
> db.class.find({$and:[{hobby:/^draw/},{hobby:/^dance/}]})
{ "_id" : ObjectId("64b28a4ffb91465cc9ea745c"), "name" : "lisi", "age" : 4, "sex" : "m", "hobby" : [ "dance", "sing", "draw" ] }
{ "_id" : ObjectId("64b28b7efb91465cc9ea745f"), "name" : "sunqi", "age" : 8, "sex" : "m", "hobby" : [ "dance", "computer", "draw" ] }

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

bash 复制代码
> db.class.find({hobby:{$size:3}})
{ "_id" : ObjectId("64b4af4b324c94c22a9d000d"), "name" : "lisi", "age" : 4, "sex" : "m", "hobby" : [ "dance", "sing", "draw" ] }
{ "_id" : ObjectId("64b4af4b324c94c22a9d000f"), "name" : "zhangsan", "age" : 6, "sex" : "m", "hobby" : [ "game", "code", "dance" ] }
{ "_id" : ObjectId("64b4b267324c94c22a9d0012"), "name" : "小红", "age" : 6, "sex" : "m", "hobby" : [ "game", "code", "draw" ] }

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

bash 复制代码
> db.class.find().sort({age:-1}).limit(1).skip(1)
{ "_id" : ObjectId("64b28a4ffb91465cc9ea745d"), "name" : "wangwu", "age" : 7, "sex" : "f", "hooby" : [ "sing", "dance", "computer" ] }

查看学生的兴趣范围:

bash 复制代码
> db.class.distinct('hobby')
[ "dance", "draw", "sing", "code", "game" ]

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

bash 复制代码
> db.class.find().sort({age:-1}).limit(3)
{ "_id" : ObjectId("64b28b7efb91465cc9ea745f"), "name" : "sunqi", "age" : 8, "sex" : "m", "hobby" : [ "dance", "computer", "draw" ] }
{ "_id" : ObjectId("64b28a4ffb91465cc9ea745d"), "name" : "wangwu", "age" : 7, "sex" : "f", "hooby" : [ "sing", "dance", "computer" ] }
{ "_id" : ObjectId("64b28a4ffb91465cc9ea745e"), "name" : "zhangsan", "age" : 6, "sex" : "m", "hobby" : [ "game", "code", "dance" ] }

删除所有 年级大于12或者小于4岁的学生:

bash 复制代码
> db.class.remove({$or: [{age:{$lt:4}},{age:{$gt:12}}]})
WriteResult({ "nRemoved" : 0 })

将小红的年龄变为8岁 兴趣爱好变为 跳舞 画画:

bash 复制代码
> db.class.update({name:'小红'},{$set:{age:8,hobby:['dance','draw']}})
#RES
> db.class.find()
{ "_id" : ObjectId("64b4b267324c94c22a9d0012"), "name" : "小红", "age" : 8, "sex" : "m", "hobby" : [ "dance", "draw" ] }

追加小明兴趣爱好 唱歌:

bash 复制代码
> db.class.update({name:'小明'},{$push:{hobby:'sing'}})

小王兴趣爱好增加 吹牛 打篮球:

bash 复制代码
> db.class.update({name:'小王'},{$pushAll:{hobby:['吹牛','basketball']}})

小李增加爱好,跑步和唱歌,但是不要和以前的重复:

bash 复制代码
> db.class.update({name:'小王'},{$addToSet:{hobby:{$each:['running','sing']}}})

该班所有同学年龄加1:

bash 复制代码
> db.class.update({},{$inc:{age:1}},false,true)

删除小明的sex属性:

bash 复制代码
> db.class.update({name:'小明'},{$unset:{sex:0}})

删除小李兴趣中的第一项:

bash 复制代码
db.class.update({name:'小李'},{$pop:{hobby:-1}})

将小红兴趣中的画画爱好删除:

bash 复制代码
> db.class.update({name:'小红'},{$pull:{hobby:'draw'}})

增加分数域 score:{'chinese':88,'english':78,'math':98}:

bash 复制代码
> db.class.update({},{$set:{score:{'chinese':88,'english':78,'math':98}}})

按照性别分组统计每组人数:

bash 复制代码
> db.class.aggregate({$group:{_id:'sex',num:{$sum:1}}})
{ "_id" : "sex", "num" : 6 }

按照姓名分组,过滤出有重名的同学:

bash 复制代码
> db.class.aggregate([{$group:{_id:'$name',num:{$sum:1}}},{$match:{num:{$gt:1}}}])

统计每名男生的语文成绩:

bash 复制代码
> db.class.aggregate([{$match:{sex:'m'}},{$project:{_id:0,name:1,'score.chinese':1}}])
{ "name" : "lisi", "score" : { "chinese" : 88 } }
{ "name" : "zhangsan" }
{ "name" : "小李", "score" : { "chinese" : 88 } }
{ "name" : "小红", "score" : { "chinese" : 88 } }

将女生按照英语分数降序排列:

bash 复制代码
> db.class.aggregate([{$match:{sex:'f'}},{$sort:{'score.english':-1}}])
{ "_id" : ObjectId("64b4af4b324c94c22a9d000e"), "name" : "wangwu", "age" : 8, "sex" : "f", "hooby" : [ "sing", "dance", "computer" ], "score" : { "chinese" : 88, "english" : 78, "math" : 98 } }

注:

文章有问题请评论或联系更正:wgq3135@163.com

相关推荐
一行玩python15 分钟前
SQLAlchemy,ORM的Python标杆!
开发语言·数据库·python·oracle
MXsoft61834 分钟前
华为服务器(iBMC)硬件监控指标解读
大数据·运维·数据库
TheITSea1 小时前
云服务器宝塔安装静态网页 WordPress、VuePress流程记录
java·服务器·数据库
王ASC2 小时前
ORA-01461: 仅能绑定要插入 LONG 列的 LONG 值。ojdbc8版本23.2.0.0驱动BUG【已解决】
数据库·sql·oracle
Dlwyz2 小时前
问题: redis-高并发场景下如何保证缓存数据与数据库的最终一致性
数据库·redis·缓存
如意机反光镜裸3 小时前
如何快速将Excel数据导入到SQL Server数据库
数据库
飞升不如收破烂~3 小时前
redis的List底层数据结构 分别什么时候使用双向链表(Doubly Linked List)和压缩列表(ZipList)
redis
不爱学习的啊Biao3 小时前
初识mysql数据库
数据库·mysql·oracle
宛唐羁客4 小时前
ODBC连接PostgreSQL数据库后,网卡DOWN后,客户端进程阻塞问题解决方法
网络·数据库
吴半杯4 小时前
Redis-monitor安装与配置
数据库·redis·缓存