8月10日 分布式缓存 周五

//创建集合
db.createCollection("集合名称")//显示创建
db.集合.insert({key:value})//隐式创建
删除指定的集合
db.集合.drop()
删除当前使用的数据库
模糊查询
> db.c1.find({name:{$regex:"n1"}})
^value开头 value$结尾
> db.c1.find({name:{$exists:1}})存在
数组的操作
> db.c1.update({age:2},{$push:{array01:1}})
push添加单个元素可添加重复元素

> db.c1.update({age:3},{$pushAll:{array02:[1,2,3,4,5]}})
多个元素添加
> db.c1.update({age:2},{$addToSet:{array01:2}})
单个元素添加并且不能添加重复元素
> db.c1.update({age:2},{$pop:{array01:1}})
array的值为整数,就从尾部删除一个元素,负数从头部
> db.c1.update({age:3},{$pull:{array02:3}})
条件删除
> db.c1.update({age:3},{$pullAll:{array02:[2,4]}})
多个条件删除删除2,4

> db.c1.update({array02:1},{$set:{"array02.1":6}})
修改数组,查询条件必须是数组,$代表通配符,匹配到第一个满足查询条件的元素,进行修改,只修改一个。$是下标

> db.c1.aggregate([{$group:{_id:"$type",c1_avg:{$avg:"$age"}}}])
{ "_id" : "002", "c1_avg" : 7 }
{ "_id" : "001", "c1_avg" : 7.25 }
> db.c1.aggregate([{$group:{_id:"$type",c1_max:{$max:"$age"}}}])
{ "_id" : "002", "c1_max" : 13 }
{ "_id" : "001", "c1_max" : 14 }
> db.c1.aggregate([{$group:{_id:"$type",c1_min:{$min:"$age"}}}])
{ "_id" : "002", "c1_min" : 1 }
{ "_id" : "001", "c1_min" : 2 }
> db.c1.aggregate([{$group:{_id:"$type",c1_sum:{$sum:"$age"}}}])
{ "_id" : "002", "c1_sum" : 49 }
{ "_id" : "001", "c1_sum" : 58 }
> db.c1.aggregate([{$group:{_id:"$type",c1_sum:{$push:"$age"}}}])
{ "_id" : "002", "c1_sum" : [ 5, 7, 9, 11, 13, 3, 1 ] }
{ "_id" : "001", "c1_sum" : [ 2, 2, 4, 6, 8, 10, 12, 14 ] }
> db.c1.aggregate([{$group:{_id:"$type",c1_push:{$push:"$age"}}}])
{ "_id" : "002", "c1_push" : [ 5, 7, 9, 11, 13, 3, 1 ] }
{ "_id" : "001", "c1_push" : [ 2, 2, 4, 6, 8, 10, 12, 14 ] }
> db.c1.aggregate([{$group:{_id:"$type",c1_first:{$first:"$age"}}}])
{ "_id" : "002", "c1_first" : 5 }
{ "_id" : "001", "c1_first" : 2 }
> db.c1.aggregate([{$group:{_id:"$type",c1_last:{$last:"$age"}}}])
{ "_id" : "002", "c1_last" : 1 }
{ "_id" : "001", "c1_last" : 14 }

索引
db.集合.ensureIndex({key:1})
固定集合
> db.createCollection("c2",{capped:true,size:100,max:20})

备份
C:\Users\hao>mongodump -h 127.0.0.1 -d db20180809 -o d:/dump

恢复
C:\Users\hao>mongorestore -h 127.0.0.1 -d db20180810 d:/dump/db20180809/c1.bson

导出
C:\Users\hao>mongoexport -h 127.0.0.1 -d db20180810 -c c1 -o d:/mongo_export/c1.json
导入
C:\Users\hao>mongoimport -h 127.0.0.1 -d db20180810 -c c2  d:/mongo_export/c1.json

创建管理员
> db.createUser({user:"root",pwd:"123456",roles:["root"]})
创建用户
> db.createUser({user:"user01",pwd:"123456",roles:["read"]})

主从复制
D:\>mongod -f D:\yourpath\config\master.conf
D:\>mongod -f D:\yourpath\config\slave1.conf
D:\>mongod -f D:\yourpath\config\slave2.conf
C:\Users\hao>mongo localhost:10000
C:\Users\hao>mongo localhost:10001
> rs.slaveOk()
C:\Users\hao>mongo localhost:10002
> rs.slaveOk()

如果你以为一哭就天下无难事,那就大错特错了。
《蜡笔小新》
——野原新之助

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值