set str 68
get str
get str
decr str
strlen str
lpush city shanghai suzhou hangzhou
lpop city
lpush name zhnagsan lisi wngwu
rpoplpush name number
rpushx city xian
llen city
hmset order id 1 customer_name "张三"
hmset gagale name lisi age 68 gender nan
hkeys order
hvals order
hmget order id
exists name
keys n*
pttl city
scan 0
scan 0 match n*
举例说明list和hash的应用场景,每个至少一个场景
List的应用场景-消息队列(Message Queue):
List适用于构建简单的消息队列,特别是在需要处理实时或异步任务时。你可以使用List的左侧(或右侧)作为队列的入口,将消息按照一定的顺序添加到List中。消费者可以从List的右侧(或左侧)获取消息,并在处理后将其移除。这样可以实现基本的消息发布和订阅模式。
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.insert([
{name:"zhang",age:10,sex:'m',hobby:['a','b','c']},
.....
])
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}]});
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.aggregate([{$group:{_id: "$sex",count:{$sum:1}}}])
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}}])