Redis
1. Command operation of string type data:
(1) Set the key value:
127.0.0.1:6379> set key1 1
(2) Read the key value:
127.0.0.1:6379> get key1
"1"
(3) The value type is incremented by 1:
127.0.0.1:6379> incr key1
(integer) 2
(4) The value type is decremented by 1:
127.0.0.1:6379> decr key1
(integer) 1
(5) View the length of the value:
127.0.0.1:6379> strlen key1
(integer) 1
2. Command operation of list type data:
(1) Insert elements into the list city: Shanghai Suzhou Hangzhou
127.0.0.1:6379> lpush city Shanghai Suzhou Hangzhou
(integer) 3
127.0.0.1:6379> lrange city 0 -1
1) "Hangzhou"
2) "Suzhou"
3) "Shanghai"
(2) Remove the head element in the list city
127.0.0.1:6379> lpop city
"Hangzhou"
127.0.0.1:6379> lrange city 0 -1
1) "Suzhou"
2) "Shanghai"
(3) Remove the tail element of the name list to the head of the number list
#操作前
127.0.0.1:6379> lrange name 0 -1
1) "lisi"
2) "zhangsan"
127.0.0.1:6379> lrange number 0 -1
1) "zhangwuji"
2) "wangwu"
#将name最后一个数据移到number中
127.0.0.1:6379> rpoplpush name number
"zhangsan"
#操作后
127.0.0.1:6379> lrange number 0 -1
1) "zhangsan"
2) "zhangwuji"
3) "wangwu"
127.0.0.1:6379> lrange name 0 -1
1) "lisi"
(4) Insert new elements into an existing list
127.0.0.1:6379> rpushx name zhangfei
(integer) 2
127.0.0.1:6379> lrange name 0 -1
1) "lisi"
2) "zhangfei"
(5) View the value length of the list
127.0.0.1:6379> llen list
(integer) 0
127.0.0.1:6379> llen name
(integer) 2
3. Command operation of hash type data:
(1) Set up a hash table, the key value information included in the order table is: id: 1, customer_name: Zhang San
127.0.0.1:6379> hset order id 1 customer_name '张三'
(integer) 2
[root@localhost ~]# redis-cli --raw
127.0.0.1:6379> hgetall order
id
1
customer_name
张三
(2) Create a hash table, and insert the key values in the table in batches
127.0.0.1:6379> hmset key2 id 1 name zhangsan age 24
OK
127.0.0.1:6379> hgetall key2
id
1
name
zhangsan
age
24
(3) Get all the keys of the map corresponding to the order
127.0.0.1:6379> hkeys order
id
customer_name
(4) Get the number of key values of the map corresponding to the order
127.0.0.1:6379> hlen order
2
(5) Get the id value in the order table
127.0.0.1:6379> hmget order id
1
4. Keys-related command operations
(1) Check if the key exists
#存在返回1,不存在返回0
127.0.0.1:6379> exists key1
1
(2) Find keys that satisfy the pattern
127.0.0.1:6379> keys key*
key1
key2
(3) Check the timeout time of the key
127.0.0.1:6379> expire key1 20
1
127.0.0.1:6379> ttl key1
-2
#如果键存在且具有超时时间,则返回剩余时间。
#如果键不存在或者没有设置超时时间(永久有效),则返回 -1。
#如果键存在但已过期(超时时间为 0),则返回 -2。
(4) traverse key
127.0.0.1:6379> keys *
order
key2
MongoDB
1. Create a database name grade
> use grade
switched to db grade
2. Create a collection name class in the database
3. Insert several data documents into the collection. The format is as follows
{name:‘zhang’,age;10,sex:‘m’,hobby:[‘a’,‘b’,‘c’]}
hobby: draw sing dance basketball football pingpong
computer
db.class.insertMany([
{ name: "小红", age: 10, sex: 'f', hobby: ['draw', 'sing', 'basketball'] },
{ name: "小明", age: 10, sex: 'm', hobby: ['dance', 'basketball', 'football'] },
{ name: "小王", age:11 , sex: 'm', hobby: ['draw', 'sing', 'dance'] },
{ name: "小李", age: 7, sex: 'm', hobby: ['computer', 'sing', 'pingpong'] },
{ name: "小赵", age: 4, sex: 'f', hobby: ['draw', 'football', 'dance'] },
{ name: "小鬼", age: 6, sex: 'm', hobby: ['sing', 'computer'] },
{ name: "小猪", age: 8, sex: 'm', hobby: ['pingpong'] },
{ name: "小杨", age: 9, sex: 'f', hobby: ['draw', 'pingpong', 'football'] },
{ name: "小天", age: 7, sex: 'm', hobby: ['computer','dance'] },
{ name: "小楚", age: 9, sex: 'f', hobby: ['sing', 'pingpong'] },
{ name: "小吴", age: 12, sex: 'm', hobby: ['sing', 'computer'] },
{ name: "小豪", age: 11, sex: 'm', hobby: ['football'] }
])
4. Find exercises
View class owner information
> db.class.find({},{_id:0})
{ "name" : "小红", "age" : 10, "sex" : "f", "hobby" : [ "draw", "sing", "basketball" ] }
{ "name" : "小明", "age" : 10, "sex" : "m", "hobby" : [ "dance", "basketball", "football" ] }
{ "name" : "小王", "age" : 11, "sex" : "m", "hobby" : [ "draw", "sing", "dance" ] }
{ "name" : "小李", "age" : 7, "sex" : "m", "hobby" : [ "computer", "sing", "pingpong" ] }
{ "name" : "小赵", "age" : 4, "sex" : "f", "hobby" : [ "draw", "football", "dance" ] }
{ "name" : "小鬼", "age" : 6, "sex" : "m", "hobby" : [ "sing", "computer" ] }
{ "name" : "小猪", "age" : 8, "sex" : "m", "hobby" : [ "pingpong" ] }
{ "name" : "小杨", "age" : 9, "sex" : "f", "hobby" : [ "draw", "pingpong", "football" ] }
{ "name" : "小天", "age" : 7, "sex" : "m", "hobby" : [ "computer", "dance" ] }
{ "name" : "小楚", "age" : 9, "sex" : "f", "hobby" : [ "sing", "pingpong" ] }
{ "name" : "小吴", "age" : 12, "sex" : "m", "hobby" : [ "sing", "computer" ] }
{ "name" : "小豪", "age" : 11, "sex" : "m", "hobby" : [ "football" ] }
View information about students aged 8 in the class
> db.class.find({age:8},{_id:0})
{ "name" : "小猪", "age" : 8, "sex" : "m", "hobby" : [ "pingpong" ] }
View information on students older than 10
> db.class.find({age:{$gt:10}},{_id:0})
{ "name" : "小王", "age" : 11, "sex" : "m", "hobby" : [ "draw", "sing", "dance" ] }
{ "name" : "小吴", "age" : 12, "sex" : "m", "hobby" : [ "sing", "computer" ] }
{ "name" : "小豪", "age" : 11, "sex" : "m", "hobby" : [ "football" ] }
View information on students aged 4-8
> db.class.find({age:{$gte:4,$lte:8}},{_id:0})
{ "name" : "小李", "age" : 7, "sex" : "m", "hobby" : [ "computer", "sing", "pingpong" ] }
{ "name" : "小赵", "age" : 4, "sex" : "f", "hobby" : [ "draw", "football", "dance" ] }
{ "name" : "小鬼", "age" : 6, "sex" : "m", "hobby" : [ "sing", "computer" ] }
{ "name" : "小猪", "age" : 8, "sex" : "m", "hobby" : [ "pingpong" ] }
{ "name" : "小天", "age" : 7, "sex" : "m", "hobby" : [ "computer", "dance" ] }
Find students whose age is 6 and who are boys
> db.class.find({age:6,sex:'m'},{_id:0})
{ "name" : "小鬼", "age" : 6, "sex" : "m", "hobby" : [ "sing", "computer" ] }
Find students younger than 7 or older than 10
> db.class.find({$or:[{age:{$lt:7}},{age:{$gt:10}}]},{_id:0})
{ "name" : "小王", "age" : 11, "sex" : "m", "hobby" : [ "draw", "sing", "dance" ] }
{ "name" : "小赵", "age" : 4, "sex" : "f", "hobby" : [ "draw", "football", "dance" ] }
{ "name" : "小鬼", "age" : 6, "sex" : "m", "hobby" : [ "sing", "computer" ] }
{ "name" : "小吴", "age" : 12, "sex" : "m", "hobby" : [ "sing", "computer" ] }
{ "name" : "小豪", "age" : 11, "sex" : "m", "hobby" : [ "football" ] }
Find students whose age is 8 or 11
> db.class.find({$or:[{age:8},{age:11}]},{_id:0})
{ "name" : "小王", "age" : 11, "sex" : "m", "hobby" : [ "draw", "sing", "dance" ] }
{ "name" : "小猪", "age" : 8, "sex" : "m", "hobby" : [ "pingpong" ] }
{ "name" : "小豪", "age" : 11, "sex" : "m", "hobby" : [ "football" ] }
Find students with two interests
> db.class.find({ $where: "this.hobby.length == 2" }, { _id: 0 })
{ "name" : "小鬼", "age" : 6, "sex" : "m", "hobby" : [ "sing", "computer" ] }
{ "name" : "小天", "age" : 7, "sex" : "m", "hobby" : [ "computer", "dance" ] }
{ "name" : "小楚", "age" : 9, "sex" : "f", "hobby" : [ "sing", "pingpong" ] }
{ "name" : "小吴", "age" : 12, "sex" : "m", "hobby" : [ "sing", "computer" ] }
Find students who are interested in drawing
> db.class.find({ hobby:'draw' }, { _id: 0 })
{ "name" : "小红", "age" : 10, "sex" : "f", "hobby" : [ "draw", "sing", "basketball" ] }
{ "name" : "小王", "age" : 11, "sex" : "m", "hobby" : [ "draw", "sing", "dance" ] }
{ "name" : "小赵", "age" : 4, "sex" : "f", "hobby" : [ "draw", "football", "dance" ] }
{ "name" : "小杨", "age" : 9, "sex" : "f", "hobby" : [ "draw", "pingpong", "football" ] }
Find students who like to draw as well as dance
> db.class.find({hobby:{$all:['draw','dance']}}, { _id: 0 })
{ "name" : "小王", "age" : 11, "sex" : "m", "hobby" : [ "draw", "sing", "dance" ] }
{ "name" : "小赵", "age" : 4, "sex" : "f", "hobby" : [ "draw", "football", "dance" ] }
Count the number of students who have three hobbies
> db.class.find({ $where: "this.hobby.length == 3" }, { _id: 0 })
{ "name" : "小红", "age" : 10, "sex" : "f", "hobby" : [ "draw", "sing", "basketball" ] }
{ "name" : "小明", "age" : 10, "sex" : "m", "hobby" : [ "dance", "basketball", "football" ] }
{ "name" : "小王", "age" : 11, "sex" : "m", "hobby" : [ "draw", "sing", "dance" ] }
{ "name" : "小李", "age" : 7, "sex" : "m", "hobby" : [ "computer", "sing", "pingpong" ] }
{ "name" : "小赵", "age" : 4, "sex" : "f", "hobby" : [ "draw", "football", "dance" ] }
{ "name" : "小杨", "age" : 9, "sex" : "f", "hobby" : [ "draw", "pingpong", "football" ] }
Find the second oldest student in the class
> db.class.find({},{_id:0}).sort({age:-1}).skip(1).limit(1)
{ "name" : "小王", "age" : 11, "sex" : "m", "hobby" : [ "draw", "sing", "dance" ] }
View student areas of interest
> db.class.distinct('hobby')
[
"basketball",
"computer",
"dance",
"draw",
"football",
"pingpong",
"sing"
]
Sort the students by age to find the oldest three
> db.class.find({},{_id:0}).sort({age:-1}).limit(3)
{ "name" : "小吴", "age" : 12, "sex" : "m", "hobby" : [ "sing", "computer" ] }
{ "name" : "小王", "age" : 11, "sex" : "m", "hobby" : [ "draw", "sing", "dance" ] }
{ "name" : "小豪", "age" : 11, "sex" : "m", "hobby" : [ "football" ] }
Delete all students older than 11 or younger than 5
> db.class.deleteMany({$or:[{age:{$gt:11}},{age:{$lt:5}}]})
{ "acknowledged" : true, "deletedCount" : 2 }
> db.class.find()
{ "_id" : ObjectId("64b27b17424df594907ba6b1"), "name" : "小红", "age" : 10, "sex" : "f", "hobby" : [ "draw", "sing", "basketball" ] }
{ "_id" : ObjectId("64b27b17424df594907ba6b2"), "name" : "小明", "age" : 10, "sex" : "m", "hobby" : [ "dance", "basketball", "football" ] }
{ "_id" : ObjectId("64b27b17424df594907ba6b3"), "name" : "小王", "age" : 11, "sex" : "m", "hobby" : [ "draw", "sing", "dance" ] }
{ "_id" : ObjectId("64b27b17424df594907ba6b4"), "name" : "小李", "age" : 7, "sex" : "m", "hobby" : [ "computer", "sing", "pingpong" ] }
{ "_id" : ObjectId("64b27b17424df594907ba6b6"), "name" : "小鬼", "age" : 6, "sex" : "m", "hobby" : [ "sing", "computer" ] }
{ "_id" : ObjectId("64b27b17424df594907ba6b7"), "name" : "小猪", "age" : 8, "sex" : "m", "hobby" : [ "pingpong" ] }
{ "_id" : ObjectId("64b27b17424df594907ba6b8"), "name" : "小杨", "age" : 9, "sex" : "f", "hobby" : [ "draw", "pingpong", "football" ] }
{ "_id" : ObjectId("64b27b17424df594907ba6b9"), "name" : "小天", "age" : 7, "sex" : "m", "hobby" : [ "computer", "dance" ] }
{ "_id" : ObjectId("64b27b17424df594907ba6ba"), "name" : "小楚", "age" : 9, "sex" : "f", "hobby" : [ "sing", "pingpong" ] }
{ "_id" : ObjectId("64b27b17424df594907ba6bc"), "name" : "小豪", "age" : 11, "sex" : "m", "hobby" : [ "football" ] }
Add, update, delete jobs:
Use the previous grade database
- Change Xiaohong's age to 8 years old and her hobbies to dance and draw
> db.class.update({
'name':'小红'},{$set:{
'age':8,'hobby':['dance','draw']}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
- Add Xiao Ming's hobbies and hobbies to sing
> db.class.update({
'name':'小明'},{$push:{
'hobby':'sing'}})
- Xiao Wang's hobbies have increased, bragging and playing basketball
> db.class.update({
'name':'小王'},{$push:{
'hobby':{$each:['basketball','吹牛']}}})
#或者,但$pushall在 MongoDB 4.2 版本后已被废弃
> db.class.update({
'name':'小王'},{$pushAll:{
'hobby':['basketball','吹牛']}})
- Xiao Li adds hobbies, running and singing, but don't repeat the same as before
> db.class.update({
'name':'小李'},{$addToSet:{hobby:{$each:['running','sing']}}})
- Add 1 to the age of all students in the class
> db.class.update({},{$inc:{age:1}},false,true)
- Delete Xiaoming's sex attribute
> db.class.update({
'name':'小明'},{$unset:{sex:0}})
- Delete the first item in Xiaoli's interest
> db.class.update({
'name':'小李'},{$pop:{hobby:-1}})
- Delete the painting hobby in Xiaohong's interests
> db.class.update({
'name':'小红'},{$pull:{hobby:'draw'}})
Use the previous grade database
to increase the score field score: {'chinese':88,'english':78,'math':98}
db.class.update({ name: "小红" }, { $set: { score: { chinese: 88, english: 78, math: 98 } } })
db.class.update({ name: "小明" }, { $set: { score: { chinese: 92, english: 85, math: 96 } } })
db.class.update({ name: "小王" }, { $set: { score: { chinese: 80, english: 90, math: 95 } } })
db.class.update({ name: "小李" }, { $set: { score: { chinese: 75, english: 80, math: 82 } } })
db.class.update({ name: "小赵" }, { $set: { score: { chinese: 85, english: 70, math: 89 } } })
db.class.update({ name: "小鬼" }, { $set: { score: { chinese: 78, english: 82, math: 85 } } })
db.class.update({ name: "小猪" }, { $set: { score: { chinese: 92, english: 85, math: 76 } } })
db.class.update({ name: "小杨" }, { $set: { score: { chinese: 89, english: 92, math: 78 } } })
db.class.update({ name: "小天" }, { $set: { score: { chinese: 86, english: 80, math: 90 } } })
db.class.update({ name: "小楚" }, { $set: { score: { chinese: 93, english: 88, math: 94 } } })
db.class.update({ name: "小吴" }, { $set: { score: { chinese: 95, english: 90, math: 85 } } })
db.class.update({ name: "小豪" }, { $set: { score: { chinese: 80, english: 85, math: 90 } } })
- Statistics of the number of people in each group according to gender
> db.class.aggregate([ { $group: { _id: "$sex", count: { $sum: 1 } } } ])
{ "_id" : "f", "count" : 3 }
{ "_id" : null, "count" : 1 }
{ "_id" : "m", "count" : 6 }
- Group by name to filter out students with the same name
#可以去更改一个重名的测试
> db.class.aggregate([{$group:{_id:'$name',num:{$sum:1}}},{$match:{num:{$gt:1}}}])
- Statistics of each boy's language performance
> db.class.aggregate([{$match:{sex:'m'}},{$project:{_id:0,name:1,'score.chinese':1}}])
{ "name" : "小王", "score" : { "chinese" : 80 } }
{ "name" : "小李", "score" : { "chinese" : 75 } }
{ "name" : "小鬼", "score" : { "chinese" : 78 } }
{ "name" : "小猪", "score" : { "chinese" : 92 } }
{ "name" : "小天", "score" : { "chinese" : 86 } }
{ "name" : "小豪", "score" : { "chinese" : 80 } }
- Arrange girls in descending order of English scores
> db.class.aggregate([{$match:{sex:'f'}},{$sort:{
'score.english':-1}}])
{
"name" : "小杨", "age" : 10, "sex" : "f", "hobby" : [ "draw", "pingpong", "football" ], "score" : { "chinese" : 89, "english" : 92, "math" : 78 } }
{
"name" : "小楚", "age" : 10, "sex" : "f", "hobby" : [ "sing", "pingpong" ], "score" : { "chinese" : 93, "english" : 88, "math" : 94 } }
{
"name" : "小红", "age" : 9, "sex" : "f", "hobby" : [ "dance" ], "score" : { "chinese" : 88, "english" : 78, "math" : 98 } }