127.0.0.1:6379[1]> set key1 1
OK
127.0.0.1:6379[1]> get key1
"1"
127.0.0.1:6379> INCR key1
(integer) 2
127.0.0.1:6379> get key1
"2"
原来的数据1 会变成 2
127.0.0.1:6379> decr key1
(integer) 1
127.0.0.1:6379> get key1
"1"
会减少 1
127.0.0.1:6379> strlen key1
(integer) 1
如果不是string类型会报错
127.0.0.1:6379> lpush list1 Shanghai Suzhou Hangzhou
(integer) 3
127.0.0.1:6379> lpop list1
"Hangzhou"
127.0.0.1:6379> rpush list1 askdj
(integer) 3
查看
127.0.0.1:6379> lrange list1 0 -1
1) "Suzhou"
2) "Shanghai"
3) "askdj"
127.0.0.1:6379> llen list1
(integer) 3
127.0.0.1:6379> hmset order id 1 custormer zhang
OK
127.0.0.1:6379> hmset order a a1 b b1 c c1
OK
127.0.0.1:6379> hgetall order
127.0.0.1:6379> hlen order
127.0.0.1:6379> hkeys order
127.0.0.1:6379> hexists order a
(integer) 1
1 代表存在
127.0.0.1:6379> ttl order
(integer) -1
-1代表永不过时
127.0.0.1:6379> lrange list1 0 -1
1) "Suzhou"
2) "Shanghai"
3) "askdj"
use grade
db.createCollection("class")
插入如下数据:
db.class.insert({name:'zhang',age;10,sex:'m',hobby:['dance','sing','draw']})
db.class.insert({name:'zhang',age:10,sex:'m',hobby:['dance','sing','draw']})
db.class.insert({name:'zhan',age:6,sex:'w',hobby:['sing','draw']})
db.class.insert({name:'zan',age:16,sex:'w',hobby:['draw']})
db.class.insert({name:'li',age:5,sex:'m',hobby:['ball','sing','draw']})
db.class.find()
db.class.find({age:8})
db.class.find({age:{$gt:10}}).pretty()
pretty() 只是让格式好看,可以不加
db.class.find({age:{$gte:4,$lte:8}})
db.class.find({age:{$eq:10},sex:"m"})
db.class.find({$or:[{age:{$gt:10,$lt:7}}]})
db.class.find({$or:[{age:{$eq:8,$eq:11}}]})
db.class.find({hobby:'draw'})
db.class.find({hobby:{$all:['dance','draw']}})
db.class.find().sort({age: -1}).skip(1).limit(1)
-1 代表降序排,skip 1 代表取第二条数据往后的数据,limit 1 代表取出第一条
db.class.find({hobby:{$size:2}})
db.class.find().sort({age: -1}).skip(0).limit(3)
db.class.find({},{_id:0,hobby:1})
db.class.deleteMany({$or:[{age:{$gt:12}},{age:{$lt:4}}]})