赞
踩
目录
(1)对列表city插入元素:Shanghai Suzhou Hangzhou
(1) 设置一个hash表,order表里包括的键值信息有:id:1,customer_name:张三
string list hash结构中,每个至少完成5个命令,包含插入 修改 删除 查询,list 和hash还需要增加遍历的操作命令
注:登录时使用--raw,就能中文输入否则会出现乱码。
set str 66
get str
incr str
decr str
strlen str
结果如下:
lpush city shanghai suzhou hangzhou
lpop city
- lpush name zhangsan lisi wangwu #创建name
- rpoplpush name number #移动
rpushx city chongqing
-
- 127.0.0.1:6379> llen city
- (integer) 3
- 127.0.0.1:6379> llen name
- (integer) 2
- 127.0.0.1:6379> llen number
- (integer) 1
- 127.0.0.1:6379>
hmset order id 1 customer_name "zhangsan"
hmset gagale name lisi age 66 gender nan
hkeys order
hvals order
hmget order id
exists name
keys n*
PTTL:是以毫秒为单位显示
TTL:是以秒为单位显示
-
- pttl city
-
- ttl city
- scan 0
-
- scan 0 match n*
-
- scan 0 match n* count 1
List类型一般用于关注人、简单队列等。
List的应用场景-消息队列(Message Queue):
List适用于构建简单的消息队列,特别是在需要处理实时或异步任务时。你可以使用List的左侧(或右侧)作为队列的入口,将消息按照一定的顺序添加到List中。消费者可以从List的右侧(或左侧)获取消息,并在处理后将其移除。这样可以实现基本的消息发布和订阅模式。
Hash类型一般用于存储用户信息、用户主页访问量、组合查询等。
Hash的应用场景-用户存储及缓存:
Hash适用于存储和缓存复杂的数据结构,如用户信息。你可以使用Hash来存储用户的属性和相应的值。每个用户可以被表示为一个独立的Hash对象,可以很方便地进行获取、更新和查询用户属性。
use grade
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: 'xiaohong', age: 7, sex: 'F', hobby: ['draw', 'dance']},
- {name: 'xiaoming', age: 8, sex: 'M', hobby: ['sing', 'basketball']},
- {name: 'xiaowang', age: 10, sex: 'M', hobby: ['pingpong', 'computer']},
- {name: 'xiaoli', age: 6, sex: 'M', hobby: ['draw', 'football']},
- {name: 'zhangsan', age: 11, sex: 'M', hobby: ['dance', 'basketball']},
- {name: 'lisi', age: 9, sex: 'M', hobby: ['draw', 'sing', 'dance']},
- {name: 'wangwu', age: 7, sex: 'F', hobby: ['pingpong', 'draw']},
- {name: 'zhaoliu', age: 12, sex: 'M', hobby: ['football', 'computer']},
- {name: 'xiaoqi', age: 5, sex: 'F', hobby: ['sing']},
- {name: 'xiaoba', age: 8, sex: 'F', hobby: ['draw', 'dance']}
- ]);
db.class.find()
db.class.find({age:8})
db.class.find({age:{$gt:10}})
db.class.find({age:{$gt:4,$lt:8}})
db.class.find({age:6,sex:"M"})
db.class.find({ $or: [ { age: { $lt: 7 } }, { age: { $gt: 10 } } ] });
db.class.find({$or:[{age:8},{age:11}]});
在MongoDB中,你可以使用查询操作符 $size
来查找具有指定长度的数组字段。
db.class.find({ hobby: { $size: 2 } });
db.class.find({ hobby:"draw" });
db.class.find({$and:[ {hobby:"draw"},{hobby:"dance"}] });
db.class.find({ hobby: { $size: 3 } }).count();
db.class.find().sort({age:-1}).skip(1).limit(1);
db.class.find({}, { "_id": 0, "hobby": 1 });
db.class.find().sort({age:-1}).limit(3);
- > db.class.deleteMany({ $or: [ { grade: { $gt: 12 } }, { age: { $lt: 4 } } ] });
- > db.class.remove({ $or: [ { grade: { $gt: 12 } }, { age: { $lt: 4 } } ] });
- db.class.update({name:"xiaohong"},
- ... {$set:{age:8,hobby:["dance","draw"]}});
db.class.update({name:"xiaoming"}, {$push:{hobby:"sing"}});
db.class.update({name:"xiaowang"}, {$push:{hobby:["basketball","brag"]}});
注:这里因为我自己的素材没有重复所以增加了篮球和画画
db.class.update({name:"xiaoli"}, {$addToSet:{hobby:{$each:["basketball","draw"]}}});
db.class.updateMany({},{$inc:{age: 1 }} );
db.class.update({name:"xiaoming"},{$unset:{sex:""}})
db.class.update({name:'xiaoli'},{$pop:{hobby:-1}});
db.class.update({name:'xiaohong'},{$pull:{hobby:'draw'}});
score:{'chinese':88,'english':78,'math':98}
注:这里因为忘了保存所以再次插入了数据。
- db.class.insertMany([
- {name: 'xiaohong', age: 7, sex: 'F', hobby: ['draw', 'dance'], score: {chinese: 91, english: 78, math: 85}},
- {name: 'xiaoming', age: 8, sex: 'M', hobby: ['sing', 'basketball'], score: {chinese: 90, english: 85, math: 73}},
- {name: 'xiaowang', age: 10, sex: 'M', hobby: ['pingpong', 'computer'], score: {chinese: 77, english: 92, math: 81}},
- {name: 'xiaoli', age: 6, sex: 'M', hobby: ['draw', 'football'], score: {chinese: 88, english: 94, math: 67}},
- {name: 'zhangsan', age: 11, sex: 'M', hobby: ['dance', 'basketball'], score: {chinese: 79, english: 82, math: 88}},
- {name: 'lisi', age: 9, sex: 'M', hobby: ['draw', 'sing', 'dance'], score: {chinese: 93, english: 71, math: 97}},
- {name: 'wangwu', age: 7, sex: 'F', hobby: ['pingpong', 'draw'], score: {chinese: 86, english: 90, math: 79}},
- {name: 'zhaoliu', age: 12, sex: 'M', hobby: ['football', 'computer'], score: {chinese: 80, english: 83, math: 92}},
- {name: 'xiaoqi', age: 5, sex: 'F', hobby: ['sing'], score: {chinese: 75, english: 87, math: 84}},
- {name: 'xiaoba', age: 8, sex: 'F', hobby: ['draw', 'dance'], score: {chinese: 94, english: 76, math: 89}}
- ]);
db.class.aggregate([{$group:{_id: "$sex",count:{$sum:1}}}])
注:因为没有重名的同学所以将xiaoqi改成了小红
- db.class.update({name:"xiaoba"},{$set:{name:'xiaohong'}});
- > db.class.aggregate([{$group: {_id:"$name",count: { $sum: 1 },names: { $push: "$name" }}}, {$match: {count: {$gt: 1 } } } ])
db.class.aggregate([{$match: {sex: "M"}},{$group: {_id: "$name",chineseScore: { $first: "$score.chinese" }}}])
db.class.aggregate([{$match: {sex: "F"}},{$sort:{"score.english":-1}}])
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。