语法不会可以在官网上查询MongoDB教程文章来源:https://www.toymoban.com/news/detail-599374.html
NoSQL练习:
Redis
String类型基本操作:
#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类型基本操作:
#对列表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类型基本操作:
#设置一个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:
> use grede
switched to db grede
#有则使用没有则创建
创建class集合:
> db.createCollection("class")
集合中插入若干数据:
文档格式如下
{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")
]
}
查看班级所有人信息:
> 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岁的学生信息:
> db.class.find({age:8})
{ "_id" : ObjectId("64b28b7efb91465cc9ea745f"), "name" : "sunqi", "age" : 8, "sex" : "m", "hobby" : [ "dance", "computer", "draw" ] }
查看年龄大于10岁的学生信息:
> db.class.find({age:{$gt:10}})
查看年龄在 4—8岁之间的学生信息:
> 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岁且为男生的学生:
> db.class.find({age:6,sex:'m'})
{ "_id" : ObjectId("64b28a4ffb91465cc9ea745e"), "name" : "zhangsan", "age" : 6, "sex" : "m", "hobby" : [ "game", "code", "dance" ] }
找到年龄小于7岁或者大于10岁的学生:
> 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岁的学生:
> db.class.find({$or: [{age:8},{age:11}]})
{ "_id" : ObjectId("64b28b7efb91465cc9ea745f"), "name" : "sunqi", "age" : 8, "sex" : "m", "hobby" : [ "dance", "computer", "draw" ] }
找到兴趣爱好有两项的学生:
db.class.find({hobby:{$size:2}})
{ "_id" : ObjectId("64b4b267324c94c22a9d0010"), "name" : "小李", "age" : 4, "sex" : "m", "hobby" : [ "dance", "sing" ] }
找到兴趣爱好有draw的学生:
> 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" ] }
找到既喜欢画画又喜欢跳舞的学生:
> 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" ] }
统计爱好有三项的学生人数:
> 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" ] }
找出本班年龄第二大的学生:
> db.class.find().sort({age:-1}).limit(1).skip(1)
{ "_id" : ObjectId("64b28a4ffb91465cc9ea745d"), "name" : "wangwu", "age" : 7, "sex" : "f", "hooby" : [ "sing", "dance", "computer" ] }
查看学生的兴趣范围:
> db.class.distinct('hobby')
[ "dance", "draw", "sing", "code", "game" ]
将学生按年龄排序找到年龄最大的三个:
> 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岁的学生:
> db.class.remove({$or: [{age:{$lt:4}},{age:{$gt:12}}]})
WriteResult({ "nRemoved" : 0 })
将小红的年龄变为8岁 兴趣爱好变为 跳舞 画画:
> 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" ] }
追加小明兴趣爱好 唱歌:
> db.class.update({name:'小明'},{$push:{hobby:'sing'}})
小王兴趣爱好增加 吹牛 打篮球:
> db.class.update({name:'小王'},{$pushAll:{hobby:['吹牛','basketball']}})
小李增加爱好,跑步和唱歌,但是不要和以前的重复:
> db.class.update({name:'小王'},{$addToSet:{hobby:{$each:['running','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'}})
增加分数域 score:{‘chinese’:88,‘english’:78,‘math’:98}:
> db.class.update({},{$set:{score:{'chinese':88,'english':78,'math':98}}})
按照性别分组统计每组人数:
> db.class.aggregate({$group:{_id:'sex',num:{$sum:1}}})
{ "_id" : "sex", "num" : 6 }
按照姓名分组,过滤出有重名的同学:
> 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}}])
{ "name" : "lisi", "score" : { "chinese" : 88 } }
{ "name" : "zhangsan" }
{ "name" : "小李", "score" : { "chinese" : 88 } }
{ "name" : "小红", "score" : { "chinese" : 88 } }
将女生按照英语分数降序排列:
> 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文章来源地址https://www.toymoban.com/news/detail-599374.html
到了这里,关于Redis,MongoDB基本操作练习题的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!