noSQL语句练习

Redis练习题

string list hash结构中,每个至少完成5个命令,包含插入 修改 删除 查询,list 和hash还需要增加遍历的操作命令
  

 1、 string类型数据的命令操作:
    (1) 设置键值:
        
127.0.0.1:6379> set key value EX 100
OK
SET key value EX 秒数 / PX 毫秒数 NX/XX

127.0.0.1:6379> SETNX key value
0

127.0.0.1:6379> SETNX key1 value
1 

127.0.0.1:6379> SETEX key2 100 value
OK
SETEX key 秒数 value

127.0.0.1:6379> PSETEX KEY3 100 VALUE
OK
PSETEX key 毫秒数 value

127.0.0.1:6379> TTL KEY4
-2

    (2) 读取键值:
        
127.0.0.1:6379> SETEX KEYA 10 "HELLO WORLD"
OK
127.0.0.1:6379> GET KEYA
HELLO WORLD

    (3) 数值类型自增1:
127.0.0.1:6379> set mykey 7
OK
127.0.0.1:6379> incr mykey
8

    (4) 数值类型自减1:
127.0.0.1:6379> set mykey 20
OK
127.0.0.1:6379> decr mykey
19

    (5) 查看值的长度:
127.0.0.1:6379> set key "this is my country"
OK
127.0.0.1:6379> strlen key
18
2、 list类型数据的命令操作:
    (1)对列表city插入元素:Shanghai Suzhou Hangzhou
        
127.0.0.1:6379> lrange city 0 8
Hangzhou
Suzhou
Shanghai
tianjing
xian
chengdu
Chongqing


    (2)将列表city里的头部的元素移除
127.0.0.1:6379> lpop city
Hangzhou
127.0.0.1:6379> lrange city 0 10
Suzhou
Shanghai
tianjing
xian
chengdu
chongqing
127.0.0.1:6379> 

    (3)将name列表的尾部元素移除到number列表的头部
127.0.0.1:6379> lrange name 0 6
Mikky
Coco
Simmons
Tom
Susan
127.0.0.1:6379> rpoplpush name number
Susan
127.0.0.1:6379> lrange name 0 -1
Mikky
Coco
Simmons
Tom
127.0.0.1:6379> lrange number 0 -1
Susan
5
4
3
2
1

    (4) 对一个已存在的列表插入新元素
127.0.0.1:6379> lrange name 0 6
Mikky
Coco
Simmons
Tom

127.0.0.1:6379> lpushx name Alice Brown
6

127.0.0.1:6379> lrange name 0 7
Brown
Alice
Mikky
Coco
Simmons
Tom

    (5)查看list的值长度
127.0.0.1:6379> llen name
6
127.0.0.1:6379> llen number
6


 3、 hash类型数据的命令操作:
     (1) 设置一个hash表,order表里包括的键值信息有:id:1,customer_name:张三
127.0.0.1:6379> hset order id "1" 
    (integer) 1
    127.0.0.1:6379> hset order customer_name "张三"
    (integer) 1

    (2) 创建一个hash表,表里的键值批量插入
 127.0.0.1:6379> hmset mykey a 1 b 2 c 3

    (3) 获取order对应的map的所有key
    127.0.0.1:6379> hkeys order

    (4) 获取order对应的map的键值数量
127.0.0.1:6379> hlen order

    (5) 获取order表里的id值
    127.0.0.1:6379> hget order id
    "1"

    
  4、Keys相关的命令操作
    (1) 查看key是否存在
127.0.0.1:6379> set name a1
OK
127.0.0.1:6379> set name b1
OK
127.0.0.1:6379> exists a1
(integer) 0
127.0.0.1:6379> exists b2
(integer) 0

    (2) 查找满足pattern的keys
127.0.0.1:6379> keys pattern
(empty array)

    (3) 查看key的超时时间
127.0.0.1:6379> ttl name
(integer) -1

    (4) 遍历key
127.0.0.1:6379> scan 0
1) "0"
2) 1) "number"
   2) "order"
   3) "mykey"
   4) "test1"
   5) "city"
   6) "name"

MangoDB练习题

1. 创建一个数据库 名字grade 
MongoDB Enterprise > use grade
switched to db grade

2. 数据库中创建一个集合名字 class
MongoDB Enterprise > db.createCollection("class");
{ "ok" : 1 }

3. 集合中插入若干数据 文档格式如下
{name:'zhang',age;10,sex:'m',hobby:['a','b','c']}
hobby: draw  sing  dance  basketball football  pingpong  computer 
MongoDB Enterprise > db.class.insert({name:'xiaozhang',age:6,sex:'m',hobby:['draw','pingpong']})
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise > db.class.insert({name:'xiaoxiang',age:9,sex:'m',hobby:['draw','basketball']})
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise > db.class.insert({name:'xiaowu',age:10,sex:'m',hobby:['sing','basketball']})
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise > db.class.insert({name:'xiaoliu',age:11,sex:'m',hobby:['footboll','dance']})
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise > db.class.insert({name:'xiaozeng',age:4,sex:'f',hobby:['footboll','dance']})
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise > db.class.insert({name:'xiaohe',age:8,sex:'f',hobby:['sing','dance']})
WriteResult({ "nInserted" : 1 })

4. 查找
查看班级所有人信息
MongoDB Enterprise > db.class.find();
{ "_id" : ObjectId("64b524c9391dfb21d3ad0776"), "name" : "xiaozhang", "age" : 6, "sex" : "m", "hobby" : [ "draw", "pingpong" ] }
{ "_id" : ObjectId("64b524d2391dfb21d3ad0777"), "name" : "xiaoxiang", "age" : 9, "sex" : "m", "hobby" : [ "draw", "basketball" ] }
{ "_id" : ObjectId("64b524e1391dfb21d3ad0778"), "name" : "xiaowu", "age" : 10, "sex" : "m", "hobby" : [ "sing", "basketball" ] }
{ "_id" : ObjectId("64b524f1391dfb21d3ad0779"), "name" : "xiaoliu", "age" : 11, "sex" : "m", "hobby" : [ "footboll", "dance" ] }
{ "_id" : ObjectId("64b5250a391dfb21d3ad077a"), "name" : "xiaozeng", "age" : 4, "sex" : "f", "hobby" : [ "footboll", "dance" ] }
{ "_id" : ObjectId("64b52528391dfb21d3ad077b"), "name" : "xiaohe", "age" : 8, "sex" : "f", "hobby" : [ "sing", "dance" ] }

查看班级中年龄为8岁的学生信息
MongoDB Enterprise > db.class.find({age:8});
{ "_id" : ObjectId("64b52528391dfb21d3ad077b"), "name" : "xiaohe", "age" : 8, "sex" : "f", "hobby" : [ "sing", "dance" ] }

查看年龄大于10岁的学生信息
MongoDB Enterprise > db.class.find({age:{$gt:10}})
{ "_id" : ObjectId("64b524f1391dfb21d3ad0779"), "name" : "xiaoliu", "age" : 11, "sex" : "m", "hobby" : [ "footboll", "dance" ] }

查看年龄在 4---8岁之间的学生信息 
MongoDB Enterprise > db.class.find({age:{$gte:4,$lte:8}})
{ "_id" : ObjectId("64b524c9391dfb21d3ad0776"), "name" : "xiaozhang", "age" : 6, "sex" : "m", "hobby" : [ "draw", "pingpong" ] }
{ "_id" : ObjectId("64b5250a391dfb21d3ad077a"), "name" : "xiaozeng", "age" : 4, "sex" : "f", "hobby" : [ "footboll", "dance" ] }
{ "_id" : ObjectId("64b52528391dfb21d3ad077b"), "name" : "xiaohe", "age" : 8, "sex" : "f", "hobby" : [ "sing", "dance" ] }

找到年龄为6岁且为男生的学生
MongoDB Enterprise > db.class.find({age:6,sex:'m'})
{ "_id" : ObjectId("64b524c9391dfb21d3ad0776"), "name" : "xiaozhang", "age" : 6, "sex" : "m", "hobby" : [ "draw", "pingpong" ] }

找到年龄小于7岁或者大于10岁的学生
MongoDB Enterprise > db.class.find({$or:[{age:{$lt:7}},{age:{$gt:10}}]})
{ "_id" : ObjectId("64b524c9391dfb21d3ad0776"), "name" : "xiaozhang", "age" : 6, "sex" : "m", "hobby" : [ "draw", "pingpong" ] }
{ "_id" : ObjectId("64b524f1391dfb21d3ad0779"), "name" : "xiaoliu", "age" : 11, "sex" : "m", "hobby" : [ "footboll", "dance" ] }
{ "_id" : ObjectId("64b5250a391dfb21d3ad077a"), "name" : "xiaozeng", "age" : 4, "sex" : "f", "hobby" : [ "footboll", "dance" ] }

找到年龄是8岁或者11岁的学生
MongoDB Enterprise > db.class.find({$or:[{age:8},{age:11}]})
{ "_id" : ObjectId("64b524f1391dfb21d3ad0779"), "name" : "xiaoliu", "age" : 11, "sex" : "m", "hobby" : [ "footboll", "dance" ] }
{ "_id" : ObjectId("64b52528391dfb21d3ad077b"), "name" : "xiaohe", "age" : 8, "sex" : "f", "hobby" : [ "sing", "dance" ] }

找到兴趣爱好有两项的学生
MongoDB Enterprise > db.class.find({$where:"this.hobby.length == 2"})
 
{ "_id" : ObjectId("64b524c9391dfb21d3ad0776"), "name" : "xiaozhang", "age" : 6, "sex" : "m", "hobby" : [ "draw", "pingpong" ] }
{ "_id" : ObjectId("64b524d2391dfb21d3ad0777"), "name" : "xiaoxiang", "age" : 9, "sex" : "m", "hobby" : [ "draw", "basketball" ] }
{ "_id" : ObjectId("64b524e1391dfb21d3ad0778"), "name" : "xiaowu", "age" : 10, "sex" : "m", "hobby" : [ "sing", "basketball" ] }
{ "_id" : ObjectId("64b524f1391dfb21d3ad0779"), "name" : "xiaoliu", "age" : 11, "sex" : "m", "hobby" : [ "footboll", "dance" ] }
{ "_id" : ObjectId("64b5250a391dfb21d3ad077a"), "name" : "xiaozeng", "age" : 4, "sex" : "f", "hobby" : [ "footboll", "dance" ] }
{ "_id" : ObjectId("64b52528391dfb21d3ad077b"), "name" : "xiaohe", "age" : 8, "sex" : "f", "hobby" : [ "sing", "dance" ] }

找到兴趣爱好有draw的学生
MongoDB Enterprise > db.class.find({hobby:'draw'})
{ "_id" : ObjectId("64b524c9391dfb21d3ad0776"), "name" : "xiaozhang", "age" : 6, "sex" : "m", "hobby" : [ "draw", "pingpong" ] }
{ "_id" : ObjectId("64b524d2391dfb21d3ad0777"), "name" : "xiaoxiang", "age" : 9, "sex" : "m", "hobby" : [ "draw", "basketball" ] }

找到既喜欢画画又喜欢跳舞的学生
MongoDB Enterprise > db.class.find({hobby:{$all:['dance','draw']}})

统计爱好有三项的学生人数
MongoDB Enterprise > db.class.find({$where:"this.hobby.length == 3"})

找出本班年龄第二大的学生
MongoDB Enterprise > db.class.find({}).sort({age:-1}).skip(1).limit(1)
{ "_id" : ObjectId("64b524e1391dfb21d3ad0778"), "name" : "xiaowu", "age" : 10, "sex" : "m", "hobby" : [ "sing", "basketball" ] }

查看学生的兴趣范围
MongoDB Enterprise > db.class.distinct('hobby')
[ "draw", "pingpong", "basketball", "sing", "dance", "footboll" 

将学生按年龄排序找到年龄最大的三个
MongoDB Enterprise > db.class.find({},{_id:0}).sort({age:-1}).limit(3)
{ "name" : "xiaoliu", "age" : 11, "sex" : "m", "hobby" : [ "footboll", "dance" ] }
{ "name" : "xiaowu", "age" : 10, "sex" : "m", "hobby" : [ "sing", "basketball" ] }
{ "name" : "xiaoxiang", "age" : 9, "sex" : "m", "hobby" : [ "draw", "basketball" ] }

删除所有 年级大于12或者小于4岁的学生
MongoDB Enterprise > db.class.deleteMany({$or:[{age:{$gt:12}},{age:{$lt:4}}]})
{ "acknowledged" : true, "deletedCount" : 0 }
5. 增加、更新、删除、统计
1. 将小红的年龄变为8岁 兴趣爱好变为 跳舞 画画
MongoDB Enterprise > db.class.update({'name':'小红'},{$set:{'age':8,'hobby':['dance','draw']}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

2. 追加小明兴趣爱好  唱歌
MongoDB Enterprise > db.class.update({'name':'小白'},{$push:{'hobby':'sing'}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

3. 小王兴趣爱好增加  吹牛 打篮球
MongoDB Enterprise > db.class.update({'name':'小王'},{$push:{'hobby':{$each:['basketball','吹牛']}}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

4. 小李增加爱好,跑步和唱歌,但是不要和以前的重复
MongoDB Enterprise > db.class.update({'name':'小珍'},{$addToSet:{hobby:{$each:['running','sing']}}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

5. 该班所有同学年龄加1
MongoDB Enterprise > db.class.update({},{$inc:{age:1}},false,true)
WriteResult({ "nMatched" : 10, "nUpserted" : 0, "nModified" : 10 })

6. 删除小明的sex属性
MongoDB Enterprise > db.class.update({'name':'小白'},{$unset:{sex:0}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

7. 删除小李兴趣中的第一项
MongoDB Enterprise > db.class.update({'name':'小哲'},{$pop:{hobby:-1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

8. 将小红兴趣中的画画爱好删除
MongoDB Enterprise > db.class.update({'name':'小红'},{$pull:{hobby:'draw'}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

增加分数域 score:{'chinese':88,'english':78,'math':98}
1. 按照性别分组统计每组人数
MongoDB Enterprise > db.class.aggregate([ { $group: { _id: "$sex", count: { $sum: 1 } } }])
{ "_id" : null, "count" : 1 }
{ "_id" : "f", "count" : 4 }
{ "_id" : "m", "count" : 5 }

2. 按照姓名分组,过滤出有重名的同学
MongoDB Enterprise > db.class.aggregate([{$group:{_id:'$name',num:{$sum:1}}},{$match:{num:{$gt:1}}}])

3. 统计每名男生的语文成绩
MongoDB Enterprise > db.class.aggregate([{$match:{sex:'m'}},{$project:{_id:0,name:1,'score.chinese':1}}])
{ "name" : "小刚", "score" : { "chinese" : 80 } }
{ "name" : "小猛", "score" : { "chinese" : 89 } }
{ "name" : "小绿", "score" : { "chinese" : 78 } }
{ "name" : "小青", "score" : { "chinese" : 75 } }
{ "name" : "小王", "score" : { "Chinese" : 80 } }

4. 将女生按照英语分数降序排列
MongoDB Enterprise > db.class.aggregate([{$match:{sex:'f'}},{$sort:{'score.english':-1}}])
 
{ "_id" : ObjectId("64a5459ca1a01a01ed810f6c"), "name" : "小红", "age" : 9, "sex" : "f", "hobby" : [ "dance" ], "score" : { "chinese" : 95, "english" : 90, "math" : 85 } }
{ "_id" : ObjectId("64a545d7a1a01a01ed810f6d"), "name" : "小珍", "age" : 20, "sex" : "f", "hobby" : [ "draw", "sing", "dance", "computer", "running" ], "score" : { "chinese" : 93, "english" : 88, "math" : 94 } }
{ "_id" : ObjectId("64a548f0a1a01a01ed810f74"), "name" : "小霞", "age" : 21, "sex" : "f", "hobby" : [ "basketball", "dance", "draw" ], "score" : { "chinese" : 92, "english" : 85, "math" : 76 } }
{ "_id" : ObjectId("64a5463ea1a01a01ed810f6f"), "name" : "小芳", "age" : 21, "sex" : "f", "hobby" : [ "draw", "sing" ], "score" : { "chinese" : 86, "english" : 80, "math" : 90 } }

你可能感兴趣的:(nosql,数据库)