目录
[1、 string类型数据的命令操作:](#1、 string类型数据的命令操作:)
[2、 list类型数据的命令操作:](#2、 list类型数据的命令操作:)
[3、 hash类型数据的命令操作:](#3、 hash类型数据的命令操作:)
[1. 创建一个数据库 名字grade](#1. 创建一个数据库 名字grade)
[2. 数据库中创建一个集合名字 class](#2. 数据库中创建一个集合名字 class)
[3. 集合中插入若干数据 文档格式如下](#3. 集合中插入若干数据 文档格式如下)
[4. 查找](#4. 查找)
[5. 增加、更新、删除、统计](#5. 增加、更新、删除、统计)
Redis:
1、 string类型数据的命令操作:
(1) 设置键值:
(2) 读取键值:
(3) 数值类型自增1:
(4) 数值类型自减1:
(5) 查看值的长度:
127.0.0.1:6379> SET name 'erHua' #(1) 设置键值:
OK
127.0.0.1:6379> GET name #(2) 读取键值:
"erHua"
127.0.0.1:6379> SET num 7
OK
127.0.0.1:6379> INCR num #(3) 数值类型自增1:
(integer) 8
127.0.0.1:6379> GET num
"8"
127.0.0.1:6379> DECR num #(4) 数值类型自减1:
(integer) 7
127.0.0.1:6379> GET num
"7"
127.0.0.1:6379> STRLEN name #(5) 查看值的长度:
(integer) 5
127.0.0.1:6379> STRLEN num
(integer) 1
注意:
127.0.0.1:6379> SET name '二花'
OK
127.0.0.1:6379> GET name
"\xe4\xba\x8c\xe8\x8a\xb1"
- REDIS数据库默认使用UTF-8编码进行存储,而不是直接使用可视化的中文字符。
- 因此,当在Redis中存储中文字符串"二花"时,它会以UTF-8编码进行存储,并返回相应的字节序列 "\xe4\xba\x8c\xe8\x8a\xb1"。
- 在Redis中,没有直接从脚本中调用编码转换函数的内置命令。因此,无法在Redis命令行界面直接进行解码。
- 如果想在Redis中获取可视化的中文字符,最好的办法是在使用Redis的客户端应用程序中进行解码,例如在Python、Java或JavaScript中执行相应的解码操作。这样可以更方便地处理字节序列并获得可读的中文字符。
2、 list类型数据的命令操作:
(1)对列表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)将列表city里的头部的元素移除
127.0.0.1:6379> lpop city
"Hangzhou"
127.0.0.1:6379> lrange city 0 -1
1) "Suzhou"
2) "Shanghai"
(3)将name列表的尾部元素移除到number列表的头部
php
127.0.0.1:6379> LPUSH name iguBigu UmsyDipsy MacaBaca TomBlyber
127.0.0.1:6379> LPUSH 1 3 5 7 9
127.0.0.1:6379> MULTI #在不知道name会弹出什么值时不使用事务
OK
127.0.0.1:6379(TX)> RPOP name
QUEUED
127.0.0.1:6379(TX)> LPUSH number iguBigu
QUEUED
127.0.0.1:6379(TX)> EXEC
1) "iguBigu"
2) (integer) 6
127.0.0.1:6379> lrange number 0 -1
1) "iguBigu"
2) "9"
3) "7"
4) "5"
5) "3"
6) "1"
127.0.0.1:6379> lrange name 0 -1
1) "TomBlyber"
2) "MacaBaca"
3) "UmsyDipsy
(4) 对一个已存在的列表插入新元素
php
127.0.0.1:6379> LPUSHX name LittleDot #插入一个小点点
(integer) 4
127.0.0.1:6379> lrange name 0 -1
1) "LittleDot"
2) "TomBlyber"
3) "MacaBaca"
4) "UmsyDipsy"
(5)查看list的值长度
php
127.0.0.1:6379> LLEN name
(integer) 4
3、 hash类型数据的命令操作:
(1) 设置一个hash表,order表里包括的键值信息有:id:1,customer_name:张三
php
127.0.0.1:6379> HSET order id 1 customer_name '张三'
(integer) 2
(2) 创建一个hash表,表里的键值批量插入
php
127.0.0.1:6379> HMSET order n1 v1 n2 v2 n3 v3 n4 v4
OK
(3) 获取order对应的map的所有key
php
127.0.0.1:6379> HKEYS order
1) "id"
2) "customer_name"
3) "n1"
4) "n2"
5) "n3"
6) "n4"
(4) 获取order对应的map的键值数量
php
127.0.0.1:6379> HLEN order
(integer) 6
(5) 获取order表里的id值
php
127.0.0.1:6379> HMGET order id
1) "1"
MongoDB:
1. 创建一个数据库 名字grade
php
> use grade
switched to db grade
> db
grade
2. 数据库中创建一个集合名字 class
php
> db.createCollection('class')
{ "ok" : 1 }
3. 集合中插入若干数据 文档格式如下
{name:'zhang',age;10,sex:'m',hobby:['a','b','c']}
hobby: draw sing dance basketball football pingpong computer
php
> db.class.insertMany([
... {name: 'LiitleDot', age: 4, sex: 'f', hobby: ['sing', 'dance']},
... {name: 'IguBigu', age: 14, sex: 'm', hobby: ['football', 'draw', 'pingpong']},
... {name: 'MacaBaca', age: 6, sex: 'm', hobby: ['draw', 'dance', 'pingpong', 'sing']},
... {name: 'TomBlyber', age: 7, sex: 'f', hobby: ['draw']},
... {name: 'UmsyDipsy', age: 11, sex: 'f', hobby: ['pingpong']},
... {name: 'LittleBean', age: 5, sex: 'm', hobby: ['pingpong', 'sing']},
... {name: 'FlyingFish', age: 8, sex: 'f', hobby: ['draw', 'dance', 'pingpong']}
... ]);
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("64b29f7744d96627bff90f54"),
ObjectId("64b29f7744d96627bff90f55"),
ObjectId("64b29f7744d96627bff90f56"),
ObjectId("64b29f7744d96627bff90f57"),
ObjectId("64b29f7744d96627bff90f58"),
ObjectId("64b29f7744d96627bff90f59"),
ObjectId("64b29f7744d96627bff90f5a")
]
}
4. 查找
查看班级所有人信息
php
> db.class.find();
{ "_id" : ObjectId("64b29f7744d96627bff90f54"), "name" : "LiitleDot", "age" : 4, "sex" : "f", "hobby" : [ "sing", "dance" ] }
{ "_id" : ObjectId("64b29f7744d96627bff90f55"), "name" : "IguBigu", "age" : 14, "sex" : "m", "hobby" : [ "football", "draw", "pingpong" ] }
{ "_id" : ObjectId("64b29f7744d96627bff90f56"), "name" : "MacaBaca", "age" : 6, "sex" : "m", "hobby" : [ "draw", "dance", "pingpong", "sing" ] }
{ "_id" : ObjectId("64b29f7744d96627bff90f57"), "name" : "TomBlyber", "age" : 7, "sex" : "f", "hobby" : [ "draw" ] }
{ "_id" : ObjectId("64b29f7744d96627bff90f58"), "name" : "UmsyDipsy", "age" : 11, "sex" : "f", "hobby" : [ "pingpong" ] }
{ "_id" : ObjectId("64b29f7744d96627bff90f59"), "name" : "LittleBean", "age" : 5, "sex" : "m", "hobby" : [ "pingpong", "sing" ] }
{ "_id" : ObjectId("64b29f7744d96627bff90f5a"), "name" : "FlyingFish", "age" : 8, "sex" : "f", "hobby" : [ "draw", "dance", "pingpong" ] }
>
查看班级中年龄为8岁的学生信息
php
> db.class.find( {age:8} );
{ "_id" : ObjectId("64b29f7744d96627bff90f5a"), "name" : "FlyingFish", "age" : 8, "sex" : "f", "hobby" : [ "draw", "dance", "pingpong" ] }
查看年龄大于10岁的学生信息
php
> db.class.find( {age:{$gt:10}} );
{ "_id" : ObjectId("64b29f7744d96627bff90f55"), "name" : "IguBigu", "age" : 14, "sex" : "m", "hobby" : [ "football", "draw", "pingpong" ] }
{ "_id" : ObjectId("64b29f7744d96627bff90f58"), "name" : "UmsyDipsy", "age" : 11, "sex" : "f", "hobby" : [ "pingpong" ] }
查看年龄在 4---8岁之间的学生信息
php
> db.class.find({age:{$gte:4,$lte:10}});
{ "_id" : ObjectId("64b29f7744d96627bff90f54"), "name" : "LiitleDot", "age" : 4, "sex" : "f", "hobby" : [ "sing", "dance" ] }
{ "_id" : ObjectId("64b29f7744d96627bff90f56"), "name" : "MacaBaca", "age" : 6, "sex" : "m", "hobby" : [ "draw", "dance", "pingpong", "sing" ] }
{ "_id" : ObjectId("64b29f7744d96627bff90f57"), "name" : "TomBlyber", "age" : 7, "sex" : "f", "hobby" : [ "draw" ] }
{ "_id" : ObjectId("64b29f7744d96627bff90f59"), "name" : "LittleBean", "age" : 5, "sex" : "m", "hobby" : [ "pingpong", "sing" ] }
{ "_id" : ObjectId("64b29f7744d96627bff90f5a"), "name" : "FlyingFish", "age" : 8, "sex" : "f", "hobby" : [ "draw", "dance", "pingpong" ] }
找到年龄为6岁且为男生的学生
php
> db.class.find( {age:6,sex:'m'} );
{ "_id" : ObjectId("64b29f7744d96627bff90f56"), "name" : "MacaBaca", "age" : 6, "sex" : "m", "hobby" : [ "draw", "dance", "pingpong", "sing" ] }
找到年龄小于7岁或者大于10岁的学生
php
> db.class.find({
... $or: [
... { age: { $lt: 7 } },
... { age: { $gt: 10 } }
... ]
... });
{ "_id" : ObjectId("64b29f7744d96627bff90f54"), "name" : "LiitleDot", "age" : 4, "sex" : "f", "hobby" : [ "sing", "dance" ] }
{ "_id" : ObjectId("64b29f7744d96627bff90f55"), "name" : "IguBigu", "age" : 14, "sex" : "m", "hobby" : [ "football", "draw", "pingpong" ] }
{ "_id" : ObjectId("64b29f7744d96627bff90f56"), "name" : "MacaBaca", "age" : 6, "sex" : "m", "hobby" : [ "draw", "dance", "pingpong", "sing" ] }
{ "_id" : ObjectId("64b29f7744d96627bff90f58"), "name" : "UmsyDipsy", "age" : 11, "sex" : "f", "hobby" : [ "pingpong" ] }
{ "_id" : ObjectId("64b29f7744d96627bff90f59"), "name" : "LittleBean", "age" : 5, "sex" : "m", "hobby" : [ "pingpong", "sing" ] }
找到年龄是8岁或者11岁的学生
php
> db.class.find({ $or: [{ age:8},{ age:11}] });
{ "_id" : ObjectId("64b29f7744d96627bff90f58"), "name" : "UmsyDipsy", "age" : 11, "sex" : "f", "hobby" : [ "pingpong" ] }
{ "_id" : ObjectId("64b29f7744d96627bff90f5a"), "name" : "FlyingFish", "age" : 8, "sex" : "f", "hobby" : [ "draw", "dance", "pingpong" ] }
找到兴趣爱好有两项的学生
php
> db.class.find({ hobby: {$size:2} });
{ "_id" : ObjectId("64b29f7744d96627bff90f54"), "name" : "LiitleDot", "age" : 4, "sex" : "f", "hobby" : [ "sing", "dance" ] }
{ "_id" : ObjectId("64b29f7744d96627bff90f59"), "name" : "LittleBean", "age" : 5, "sex" : "m", "hobby" : [ "pingpong", "sing" ] }
找到兴趣爱好有draw的学生
php
> db.class.find({ hobby:'draw' });
{ "_id" : ObjectId("64b29f7744d96627bff90f55"), "name" : "IguBigu", "age" : 14, "sex" : "m", "hobby" : [ "football", "draw", "pingpong" ] }
{ "_id" : ObjectId("64b29f7744d96627bff90f56"), "name" : "MacaBaca", "age" : 6, "sex" : "m", "hobby" : [ "draw", "dance", "pingpong", "sing" ] }
{ "_id" : ObjectId("64b29f7744d96627bff90f57"), "name" : "TomBlyber", "age" : 7, "sex" : "f", "hobby" : [ "draw" ] }
{ "_id" : ObjectId("64b29f7744d96627bff90f5a"), "name" : "FlyingFish", "age" : 8, "sex" : "f", "hobby" : [ "draw", "dance", "pingpong" ] }
找到既喜欢画画又喜欢跳舞的学生
php
> db.class.find({ hobby:{$all: ['draw','dance']} });
{ "_id" : ObjectId("64b29f7744d96627bff90f56"), "name" : "MacaBaca", "age" : 6, "sex" : "m", "hobby" : [ "draw", "dance", "pingpong", "sing" ] }
{ "_id" : ObjectId("64b29f7744d96627bff90f5a"), "name" : "FlyingFish", "age" : 8, "sex" : "f", "hobby" : [ "draw", "dance", "pingpong" ] }
统计爱好有三项的学生人数
php
> db.class.find({ hobby: {$size:3} }).count();
2
找出本班年龄第二大的学生
php
> db.class.find().sort({age:-1}).skip(1).limit(1);
{ "_id" : ObjectId("64b29f7744d96627bff90f58"), "name" : "UmsyDipsy", "age" : 11, "sex" : "f", "hobby" : [ "pingpong" ] }
查看学生的兴趣范围
php
> db.class.aggregate([
... {$unwind:'$hobby'},
... {$group: {
... _id: null,hobbies: {$addToSet:'$hobby'}
... }
... } ]);
{ "_id" : null, "hobbies" : [ "pingpong", "football", "dance", "sing", "draw" ] }
将学生按年龄排序找到年龄最大的三个
php
> db.class.aggregate([
... {$sort: {age:-1}} , {$limit:3} ,{$project:{_id:0 ,name:1 ,sex:1 ,age:1 ,hobby:1}}
... ]);
{ "name" : "IguBigu", "age" : 14, "sex" : "m", "hobby" : [ "football", "draw", "pingpong" ] }
{ "name" : "UmsyDipsy", "age" : 11, "sex" : "f", "hobby" : [ "pingpong" ] }
{ "name" : "FlyingFish", "age" : 8, "sex" : "f", "hobby" : [ "draw", "dance", "pingpong" ] }
删除所有 年纪大于14或者小于4岁的学生
php
> db.class.remove({ $or:[ {age:{$lt:4}},{age:{$gt:14} }] });
WriteResult({ "nRemoved" : 0 })
5. 增加、更新、删除、统计
- 将小点点的年龄变为8岁 兴趣爱好变为 跳舞 画画
php
> db.class.replaceOne(
... {name:'LiitleDot'},
... {name:'LiitleDot',gae:8 ,habby:['dance','draw']}
... );
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.class.find({name:'LiitleDot'});
{ "_id" : ObjectId("64b2b4a0779acd38c6a2bf81"), "name" : "LiitleDot", "gae" : 8, "habby" : [ "dance", "draw" ] }
- 追加唔西·迪西趣爱好 唱歌
php
> db.class.updateOne(
... {name: 'UmsyDipsy'},
... {$push: {hobby: 'sing'}}
... );
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.class.find({name: 'UmsyDipsy'});
{ "_id" : ObjectId("64b29f7744d96627bff90f58"), "name" : "UmsyDipsy", "gae" : 8, "sex" : "m", "hobby" : [ "draw", "sing" ] }
3.汤姆布利柏趣爱好增加 吹牛 打篮球
php
> db.class.updateOne(
... {name: 'TomBlyber'},
... {$push: {hobby: ['TalkBig', 'basketball']}}
... );
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.class.find({name:'TomBlyber'});
{ "_id" : ObjectId("64b29f7744d96627bff90f57"), "name" : "TomBlyber", "hobby" : [ "draw","TalkBig", "basketball" ] }
- 飞飞鱼增加爱好,跑步和唱歌,但是不要和以前的重复
php
> db.class.updateOne(
... {name: 'FlyingFish'},
... {$addToSet: {hobby: {$each: ['跑步', '唱歌']}}}
... );
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.class.find({name: FlyibngFish});
{ "_id" : ObjectId("64b29f7744d96627bff90f5a"), "name" : "FlyingFish", "age" : 8, "sex" : "f", "hobby" : [ "draw", "dance", "pingpong", "跑步", "唱歌" ] }
- 该班所有同学年龄加1
php
db.class.updateMany({},{$inc :{age:1}});
{ "acknowledged" : true, "matchedCount" : 7, "modifiedCount" : 7 }
> db.class.find({},{_id:0,name:1,age:1})
{ "name" : "LiitleDot", "age" : 9 }
{ "name" : "IguBigu", "age" : 15 }
{ "name" : "MacaBaca", "age" : 7 }
{ "name" : "TomBlyber", "age" : 8 }
{ "name" : "UmsyDipsy", "age" : 12 }
{ "name" : "LittleBean", "age" : 6 }
{ "name" : "FlyingFish", "age" : 9 }
- 删除小豆豆的sex属性
php
> db.class.updateOne({"name" : "LittleBean"},{$unset:{sex:1}});
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.class.find({"name" : "LittleBean"});
{ "_id" : ObjectId("64b2cce5f8a301175474d607"), "name" : "LittleBean", "age" : 6, "hobby" : [ "pingpong", "sing" ] }
- 删除玛卡·巴卡兴趣中的第一项
php
> db.class.updateOne(
... {"name" : "MacaBaca"},
... {$pop:{hobby:-1}}
... );
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.class.find( {"name" : "MacaBaca"});
{ "_id" : ObjectId("64b2cce5f8a301175474d604"), "name" : "MacaBaca", "age" : 7, "sex" : "m", "hobby" : [ "dance", "pingpong", "sing" ] }
- 将依古·比古兴趣中的画画爱好删除
php
> db.class.updateOne(
... {"name" : "IguBigu"},
... {$pull: {hobby:'draw'}}
... );
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.class.find( {"name" : "IguBigu"});
{ "_id" : ObjectId("64b2cce5f8a301175474d603"), "name" : "IguBigu", "age" : 15, "sex" : "m", "hobby" : [ "football", "pingpong" ] }
增加分数域 score:{'chinese':88,'english':78,'math':98}
- 按照性别分组统计每组人数
php
> db.class.aggregate([
... {
... $group: {_id: "$sex", count: { $sum: 1 }
... }
... }
... ]);
{ "_id" : "f", "count" : 4 }
{ "_id" : "m", "count" : 2 }
{ "_id" : null, "count" : 1 }
- 按照姓名分组,过滤出有重名的同学
php
> db.class.aggregate([
... {
... $group: {_id: "$name", count: { $sum: 1 } }
... },
...
... {
... $match: { count: { $gt: 1 } }
... },
...
... {
... $project: {_id: 0, name: "$_id", count: 1}
... }
... ]);
{ "count" : 2, "name" : "xiaowang" }
- 统计每名男生的语文成绩
php
> db.class.aggregate([
... {
... $match: {sex: "m"}
... },
... {
... $project: {_id: 0, name: "$name", "score": '$score.chinese'}
... }
... ]);
{ "name" : "IguBigu", "score" : 92 }
{ "name" : "MacaBaca", "score" : 85 }
{ "name" : "xiaowang" }
{ "name" : "xiaowang" }
- 将女生按照英语分数降序排列
php
> db.class.aggregate([
... {
... $match: {sex: "f"}
... },
... {
... $sort: {"score.english": -1}
... },
... {
... $project: {_id: 0, name: "$name", englishScore: "$score.english" }
... }
... ]);
{ "name" : "TomBlyber", "englishScore" : 91 }
{ "name" : "LiitleDot", "englishScore" : 88 }
{ "name" : "UmsyDipsy", "englishScore" : 86 }
{ "name" : "FlyingFish", "englishScore" : 58 }