目录
一、NOSQL数据库相关操作
1、string类型数据的命令操作
2、listl类型数据的命令操作
3、hash类型数据的命令操作
4、key相关的命令操作
二、MongoDB
1. 创建一个数据库 名字grade
2. 数据库中创建一个集合名字 class
3. 集合中插入若干数据 文档格式如下
4、查询(查找)
4.1、查看班级所有人信息
4.2、查看班级中年龄为8岁的学生信息
4.3、查看年龄大于10岁的学生信息
4.4、查看年龄在 4---8岁之间的学生信息
4.5、找到年龄为6岁且为男生的学生
4.6、找到年龄小于7岁或者大于10岁的学生
4.7、找到年龄是8岁或者11岁的学生
4.8、找到兴趣爱好有两项的学生
4.9、找到兴趣爱好有draw的学生
4.10、找到既喜欢画画又喜欢跳舞的学生
4.11、统计爱好有三项的学生人数
4.12、找出本班年龄第二大的学生
4.13、查看学生的兴趣范围
4.14、将学生按年龄排序找到年龄最大的三个
4.15、删除所有 年龄大于12或者小于4岁的学生
5、增加、更新、删除、统计
5.1、将小红的年龄变为8岁 兴趣爱好变为 跳舞 画画
5.2、追加小明兴趣爱好 唱歌
5.3、小王兴趣爱好增加 吹牛 打篮球
5.4、小李增加爱好,跑步和唱歌,但是不要和以前的重复
5.5、该班所有同学年龄加1
5.6、 删除小明的sex属性
5.7、删除小李兴趣中的第一项
5.8、 将小红兴趣中的画画爱好删除
6、增加分数域 并进行下列操作
6.1、按照性别分组统计每组人数
6.2、 按照姓名分组,过滤出有重名的同学
6.3、统计每名男生的语文成绩
6.4、将女生按照英语分数降序排列
1) 设置键值:
127.0.0.1:6379> set key1 1
OK
(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> strlen key1
(integer) 1
(5) 查看值的长度:
127.0.0.1:6379> set key2 hello
OK
127.0.0.1:6379> get key2
"hello"
127.0.0.1:6379> strlen key2
(integer) 5
(1)对列表city插入元素:Shanghai Suzhou Hangzhou
127.0.0.1:6379> lpush city Shanghai Suzhou Hangzhou
(integer)
(2)将列表city里的头部的元素移除
127.0.0.1:6379> lpop city
"Hangzhou"
(3)将name列表的尾部元素移除到number列表的头部
127.0.0.1:6379> rpush name 1 2 3
(integer) 3
127.0.0.1:6379> rpush number 0
(integer) 1
127.0.0.1:6379> rpoplpush name number
"3"
127.0.0.1:6379> lrange name 0 -1
1) "1"
2) "2"
127.0.0.1:6379> lrange number 0 -1
1) "3"
2) "0"
(4) 对一个已存在的列表插入新元素
127.0.0.1:6379> rpushx number 4
(integer) 3
127.0.0.1:6379> lrange number 0 -1
1) "3"
2) "0"
3) "4"
(5)查看list的值长度
127.0.0.1:6379> llen list
(integer) 7
如果中文输入显示不出来可以在登录的时候加上后缀 : --raw
(1) 设置一个hash表,order表里包括的键值信息有:id:1,customer_name:张三
127.0.0.1:6379> hmset order id 1 customer_name "张三"
OK
127.0.0.1:6379> hvals order
1
张三
(2) 创建一个hash表,表里的键值批量插入
127.0.0.1:6379> hmset order id 2 customer_name "李四" id1 3 customer_name1 "王五"
OK
127.0.0.1:6379> hvals order
2
李四
3
王五
(3) 获取order对应的map的所有key
127.0.0.1:6379> hkeys order
id
customer_name
id1
customer_name1
(4) 获取order对应的map的键值数量
127.0.0.1:6379> hlen order
4
(5) 获取order表里的id值
127.0.0.1:6379> hget order id
2
127.0.0.1:6379> hget order id1
3
(1) 查看key是否存在
127.0.0.1:6379> exists key
1
(2) 查找满足pattern的keys
127.0.0.1:6379> keys pattern
(3) 查看key的超时时间
127.0.0.1:6379> ttl key
-1
(4) 遍历key
127.0.0.1:6379> keys *
key1
list
order
key2
city
Shanghai
name
key
orders
number
> use grade
switched to db grade
{name:'zhang',age;10,sex:'m',hobby:['a','b','c']}
hobby: draw sing dance basketball football pingpong computer
> db.class.insertMany(
... [{name:"李四",age:8,sex:"男",hobby:['draw']},
... {name:"赵四",age:5,sex:"女",hobby:['draw','dance']},
... {name:"王五",age:6,sex:"男",hobby:['sing','computer','pingpong']},
... {name:"钱七",age:11,sex:"女",hobby:['dance']}]
... )
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("64a699bfaef644e7b3f5def9"),
ObjectId("64a699bfaef644e7b3f5defa"),
ObjectId("64a699bfaef644e7b3f5defb"),
ObjectId("64a699bfaef644e7b3f5defc")
]
}
> db.class.find()
{ "_id" : ObjectId("64a6988caef644e7b3f5def8"), "name" : "张三", "age" : 10, "sex" : "男", "hobby" : [ "draw", "dance" ] }
{ "_id" : ObjectId("64a699bfaef644e7b3f5def9"), "name" : "李四", "age" : 8, "sex" : "男", "hobby" : [ "draw" ] }
{ "_id" : ObjectId("64a699bfaef644e7b3f5defa"), "name" : "赵四", "age" : 5, "sex" : "女", "hobby" : [ "draw", "dance" ] }
{ "_id" : ObjectId("64a699bfaef644e7b3f5defb"), "name" : "王五", "age" : 6, "sex" : "男", "hobby" : [ "sing", "computer", "pingpong" ] }
{ "_id" : ObjectId("64a699bfaef644e7b3f5defc"), "name" : "钱七", "age" : 11, "sex" : "女", "hobby" : [ "dance" ] }
> db.class.find()
{ "_id" : ObjectId("64a6988caef644e7b3f5def8"), "name" : "张三", "age" : 10, "sex" : "男", "hobby" : [ "draw", "dance" ] }
{ "_id" : ObjectId("64a699bfaef644e7b3f5def9"), "name" : "李四", "age" : 8, "sex" : "男", "hobby" : [ "draw" ] }
{ "_id" : ObjectId("64a699bfaef644e7b3f5defa"), "name" : "赵四", "age" : 5, "sex" : "女", "hobby" : [ "draw", "dance" ] }
{ "_id" : ObjectId("64a699bfaef644e7b3f5defb"), "name" : "王五", "age" : 6, "sex" : "男", "hobby" : [ "sing", "computer", "pingpong" ] }
{ "_id" : ObjectId("64a699bfaef644e7b3f5defc"), "name" : "钱七", "age" : 11, "sex" : "女", "hobby" : [ "dance" ] }
{ "_id" : ObjectId("64a69a00aef644e7b3f5defd"), "name" : "孙八", "age" : 4, "sex" : "男", "hobby" : [ "draw", "pingpong" ] }
{ "_id" : ObjectId("64a69a00aef644e7b3f5defe"), "name" : "侯九", "age" : 13, "sex" : "男", "hobby" : [ "basketball" ] }
{ "_id" : ObjectId("64a69a00aef644e7b3f5deff"), "name" : "熊十", "age" : 7, "sex" : "女", "hobby" : [ "draw", "dance" ] }
> db.class.find({age:8})
{ "_id" : ObjectId("64a699bfaef644e7b3f5def9"), "name" : "李四", "age" : 8, "sex" : "男", "hobby" : [ "draw" ] }
> db.class.find({age:{$gt:10}})
{ "_id" : ObjectId("64a699bfaef644e7b3f5defc"), "name" : "钱七", "age" : 11, "sex" : "女", "hobby" : [ "dance" ] }
{ "_id" : ObjectId("64a69a00aef644e7b3f5defe"), "name" : "侯九", "age" : 13, "sex" : "男", "hobby" : [ "basketball" ] }
> db.class.find({age:{$gte:4,$lte:8}})
{ "_id" : ObjectId("64a699bfaef644e7b3f5def9"), "name" : "李四", "age" : 8, "sex" : "男", "hobby" : [ "draw" ] }
{ "_id" : ObjectId("64a699bfaef644e7b3f5defa"), "name" : "赵四", "age" : 5, "sex" : "女", "hobby" : [ "draw", "dance" ] }
{ "_id" : ObjectId("64a699bfaef644e7b3f5defb"), "name" : "王五", "age" : 6, "sex" : "男", "hobby" : [ "sing", "computer", "pingpong" ] }
{ "_id" : ObjectId("64a69a00aef644e7b3f5defd"), "name" : "孙八", "age" : 4, "sex" : "男", "hobby" : [ "draw", "pingpong" ] }
{ "_id" : ObjectId("64a69a00aef644e7b3f5deff"), "name" : "熊十", "age" : 7, "sex" : "女", "hobby" : [ "draw", "dance" ] }
> db.class.find({"$and":[{age:6},{"sex":"男"}]})
{ "_id" : ObjectId("64a699bfaef644e7b3f5defb"), "name" : "王五", "age" : 6, "sex" : "男", "hobby" : [ "sing", "computer", "pingpong" ] }
> db.class.find({"$or":[{age:{$gt:10}},{age:{$lt:7}}]})
{ "_id" : ObjectId("64a699bfaef644e7b3f5defa"), "name" : "赵四", "age" : 5, "sex" : "女", "hobby" : [ "draw", "dance" ] }
{ "_id" : ObjectId("64a699bfaef644e7b3f5defb"), "name" : "王五", "age" : 6, "sex" : "男", "hobby" : [ "sing", "computer", "pingpong" ] }
{ "_id" : ObjectId("64a699bfaef644e7b3f5defc"), "name" : "钱七", "age" : 11, "sex" : "女", "hobby" : [ "dance" ] }
{ "_id" : ObjectId("64a69a00aef644e7b3f5defd"), "name" : "孙八", "age" : 4, "sex" : "男", "hobby" : [ "draw", "pingpong" ] }
{ "_id" : ObjectId("64a69a00aef644e7b3f5defe"), "name" : "侯九", "age" : 13, "sex" : "男", "hobby" : [ "basketball" ] }
> db.class.find({"$or":[{age:8},{age:11}]})
{ "_id" : ObjectId("64a699bfaef644e7b3f5def9"), "name" : "李四", "age" : 8, "sex" : "男", "hobby" : [ "draw" ] }
{ "_id" : ObjectId("64a699bfaef644e7b3f5defc"), "name" : "钱七", "age" : 11, "sex" : "女", "hobby" : [ "dance" ] }
> db.class.find({hobby:{$size:2}})
{ "_id" : ObjectId("64a65d10ff846fd29644e029"), "name" : "张三", "age" : 10, "sex" : "男", "hobby" : [ "draw", "pingpong" ] }
{ "_id" : ObjectId("64a65d10ff846fd29644e02b"), "name" : "赵四", "age" : 5, "sex" : "女", "hobby" : [ "draw", "dance" ] }
{ "_id" : ObjectId("64a65d10ff846fd29644e02e"), "name" : "孙八", "age" : 4, "sex" : "男", "hobby" : [ "draw", "pingpong" ] }
{ "_id" : ObjectId("64a65d10ff846fd29644e030"), "name" : "熊十", "age" : 7, "sex" : "女", "hobby" : [ "draw", "dance" ] }
> db.class.find({hobby:'draw'})
{ "_id" : ObjectId("64a65d10ff846fd29644e029"), "name" : "张三", "age" : 10, "sex" : "男", "hobby" : [ "draw", "pingpong" ] }
{ "_id" : ObjectId("64a65d10ff846fd29644e02a"), "name" : "李四", "age" : 8, "sex" : "男", "hobby" : [ "draw" ] }
{ "_id" : ObjectId("64a65d10ff846fd29644e02b"), "name" : "赵四", "age" : 5, "sex" : "女", "hobby" : [ "draw", "dance" ] }
{ "_id" : ObjectId("64a65d10ff846fd29644e02e"), "name" : "孙八", "age" : 4, "sex" : "男", "hobby" : [ "draw", "pingpong" ] }
{ "_id" : ObjectId("64a65d10ff846fd29644e030"), "name" : "熊十", "age" : 7, "sex" : "女", "hobby" : [ "draw", "dance" ] }
> db.class.find({hobby:{$all:["draw","dance"]}})
{ "_id" : ObjectId("64a65d10ff846fd29644e02b"), "name" : "赵四", "age" : 5, "sex" : "女", "hobby" : [ "draw", "dance" ] }
{ "_id" : ObjectId("64a65d10ff846fd29644e030"), "name" : "熊十", "age" : 7, "sex" : "女", "hobby" : [ "draw", "dance" ] }
> db.class.find({hobby:{$size:3}})
{ "_id" : ObjectId("64a65d10ff846fd29644e02c"), "name" : "王五", "age" : 6, "sex" : "男", "hobby" : [ "sing", "computer", "pingpong" ] }
> db.class.find().sort({age:-1}).skip(1).limit(1)
{ "_id" : ObjectId("64a65d10ff846fd29644e02d"), "name" : "钱七", "age" : 11, "sex" : "女", "hobby" : [ "dance" ] }
> db.class.distinct("hobby")
[ "basketball", "computer", "dance", "draw", "pingpong", "sing" ]
> db.class.find().sort({age:-1}).limit(3)
{ "_id" : ObjectId("64a65d10ff846fd29644e02f"), "name" : "侯九", "age" : 13, "sex" : "男", "hobby" : [ "basketball" ] }
{ "_id" : ObjectId("64a65d10ff846fd29644e02d"), "name" : "钱七", "age" : 11, "sex" : "女", "hobby" : [ "dance" ] }
{ "_id" : ObjectId("64a65d10ff846fd29644e029"), "name" : "张三", "age" : 10, "sex" : "男", "hobby" : [ "draw", "pingpong" ] }
> db.class.remove({"$or":[{age:{$lt:4}},{age:{$gt:12}}]})
WriteResult({ "nRemoved" : 1 })
> db.class.find()
{ "_id" : ObjectId("64a65d10ff846fd29644e029"), "name" : "张三", "age" : 10, "sex" : "男", "hobby" : [ "draw", "pingpong" ] }
{ "_id" : ObjectId("64a65d10ff846fd29644e02a"), "name" : "李四", "age" : 8, "sex" : "男", "hobby" : [ "draw" ] }
{ "_id" : ObjectId("64a65d10ff846fd29644e02b"), "name" : "赵四", "age" : 5, "sex" : "女", "hobby" : [ "draw", "dance" ] }
{ "_id" : ObjectId("64a65d10ff846fd29644e02c"), "name" : "王五", "age" : 6, "sex" : "男", "hobby" : [ "sing", "computer", "pingpong" ] }
{ "_id" : ObjectId("64a65d10ff846fd29644e02d"), "name" : "钱七", "age" : 11, "sex" : "女", "hobby" : [ "dance" ] }
{ "_id" : ObjectId("64a65d10ff846fd29644e02e"), "name" : "孙八", "age" : 4, "sex" : "男", "hobby" : [ "draw", "pingpong" ] }
{ "_id" : ObjectId("64a65d10ff846fd29644e030"), "name" : "熊十", "age" : 7, "sex" : "女", "hobby" : [ "draw", "dance" ] }
> db.class1.find()
{ "_id" : ObjectId("64a66525ff846fd29644e031"), "name" : "小红", "age" : 7, "sex" : "女", "hobby" : [ "pingpong", "dance" ] }
{ "_id" : ObjectId("64a66525ff846fd29644e032"), "name" : "小明", "age" : 8, "sex" : "男", "hobby" : [ "draw", "dance" ] }
{ "_id" : ObjectId("64a66525ff846fd29644e033"), "name" : "小李", "age" : 8, "sex" : "女", "hobby" : [ "draw", "dance" ] }
{ "_id" : ObjectId("64a66525ff846fd29644e034"), "name" : "小王", "age" : 9, "sex" : "男", "hobby" : [ "draw", "dance" ] }
> db.class1.update({name:"小红"},{'$set':{age:8,hobby:["跳舞","画画"]}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.class1.find({name:"小红"})
{ "_id" : ObjectId("64a66525ff846fd29644e031"), "name" : "小红", "age" : 8, "sex" : "女", "hobby" : [ "跳舞", "画画" ] }
> db.class1.update({name:"小明"},{$push:{hobby:"唱歌"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.class1.find({name:"小明"})
{ "_id" : ObjectId("64a66525ff846fd29644e032"), "name" : "小明", "age" : 8, "sex" : "男", "hobby" : [ "draw", "dance", "唱歌" ] }
> db.class1.update({name:"小王"},{'$push':{hobby:["吹牛","打篮球"]}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.class1.find({name:"小王"})
{ "_id" : ObjectId("64a66cb3ff846fd29644e036"), "name" : "小王", "age" : 9, "sex" : "男", "hobby" : [ "draw", "sing", [ "吹牛", "打篮球" ] ] }
> db.class1.find({name:"小李"})
{ "_id" : ObjectId("64a66e61ff846fd29644e037"), "name" : "小李", "age" : 8, "sex" : "女", "hobby" : [ "draw", "sing" ] }
> db.class1.update({name:"小李"},{$addToSet:{hobby:{$each:["跑步","sing"]}}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.class1.find({name:"小李"})
{ "_id" : ObjectId("64a66e61ff846fd29644e037"), "name" : "小李", "age" : 8, "sex" : "女", "hobby" : [ "draw", "sing", "跑步" ] }
> db.class1.update({},{$inc:{age:1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.class1.find()
{ "_id" : ObjectId("64a66525ff846fd29644e031"), "name" : "小红", "age" : 9, "sex" : "女", "hobby" : [ "跳舞", "画画" ] }
{ "_id" : ObjectId("64a66525ff846fd29644e032"), "name" : "小明", "age" : 8, "sex" : "男", "hobby" : [ "draw", "dance", "唱歌" ] }
{ "_id" : ObjectId("64a66cb3ff846fd29644e036"), "name" : "小王", "age" : 9, "sex" : "男", "hobby" : [ "draw", "sing", [ "吹牛", "打篮球" ] ] }
{ "_id" : ObjectId("64a66e61ff846fd29644e037"), "name" : "小李", "age" : 8, "sex" : "女", "hobby" : [ "draw", "sing", "跑步" ] }
> db.class1.update({name:"小明"},{'$unset':{sex:1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.class1.find({name:"小明"})
{ "_id" : ObjectId("64a66525ff846fd29644e032"), "name" : "小明", "age" : 8, "hobby" : [ "draw", "dance", "唱歌" ] }
> db.class1.update({name:"小李"},{'$pop':{hobby:-1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.class1.find({name:"小李"})
{ "_id" : ObjectId("64a66e61ff846fd29644e037"), "name" : "小李", "age" : 8, "sex" : "女", "hobby" : [ "sing", "跑步" ] }
> db.class1.update({name:"小红"},{'$pull':{hobby:"draw"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })
> db.class1.find({name:"小红"})
{ "_id" : ObjectId("64a66525ff846fd29644e031"), "name" : "小红", "age" : 9, "sex" : "女", "hobby" : [ "跳舞", "画画" ] }
score:{'chinese':88,'english':78,'math':98}
{name:"张三",sex:"男",score:{chinese:78,english:79,math:98}}
{name:"李四",sex:"男",score:{chinese:98,english:98,math:98}}
{name:"赵四",sex:"男",score:{chinese:58,english:88,math:98}}
{name:"王五",sex:"女",score:{chinese:89,english:74,math:98}}
{name:"钱七",sex:"男",score:{chinese:48,english:78,math:98}}
{name:"孙八",sex:"女",score:{chinese:88,english:78,math:98}}
{name:"侯九",sex:"男",score:{chinese:86,english:75,math:98}}
{name:"熊十",sex:"女",score:{chinese:87,english:48,math:98}}
{name:"熊十",sex:"女",score:{chinese:87,english:48,math:98}}
> db.class3.aggregate({$group:{_id:'$sex',num:{$sum:1}}})
{ "_id" : "男", "num" : 5 }
{ "_id" : "女", "num" : 4 }
> db.class3.aggregate([{$group:{_id:'$name',num:{$sum:1}}},{$match:{num:{$gt:1}}}])
{ "_id" : "熊十", "num" : 2 }
> db.class3.aggregate([{$match:{sex:"男"}},{$project:{_id:0,name:1,'score.chinese':1}}])
{ "name" : "张三", "score" : { "chinese" : 78 } }
{ "name" : "李四", "score" : { "chinese" : 98 } }
{ "name" : "赵四", "score" : { "chinese" : 58 } }
{ "name" : "钱七", "score" : { "chinese" : 48 } }
{ "name" : "侯九", "score" : { "chinese" : 86 } }
> db.class3.aggregate([{$match:{sex:"女"}},{$sort:{'score.english':-1}}])
{ "_id" : ObjectId("64a6946dff846fd29644e03f"), "name" : "孙八", "sex" : "女", "score" : { "chinese" : 88, "english" : 78, "math" : 98 } }
{ "_id" : ObjectId("64a6946dff846fd29644e03d"), "name" : "王五", "sex" : "女", "score" : { "chinese" : 89, "english" : 74, "math" : 98 } }
{ "_id" : ObjectId("64a6946dff846fd29644e041"), "name" : "熊十", "sex" : "女", "score" : { "chinese" : 87, "english" : 48, "math" : 98 } }
{ "_id" : ObjectId("64a6946dff846fd29644e042"), "name" : "熊十", "sex" : "女", "score" : { "chinese" : 87, "english" : 48, "math" : 98 } }