NoSQL——MongoDB副本集和文档管理
一. 副本集
1. 介绍
- 副本集也称为MongoDB复制
- 在多个服务器上存储数据,实现数据同步
- 提高数据可用性、安全性,方便数据故障恢复
2. MongoDB复制原理
- 至少需要两个节点,其中一个是主节点,负责处理客户端请求,其余是从节点,负责复制主节点数据。
- 常见搭配:一主一从,一主多从
- 主节点记录所有操作,从节点定期轮询主节点获取这些操作,对自己的数据副本做操作,保持一致。
3. 副本集实现方式
(1) 主从复制方式(宕机后需要手动配置,已不用)
(2) Replica Sets副本集
- 1.6版本后支持,由于主从复制方式
- 故障自动切换,自动修复成员节点,降低运维成本
- 类似于高可用集群
4. 配置Replica Sets
(1) 环境准备,3台主机
- 192.168.4.51: 27051 主
- 192.168.4.52: 27052 从
- 192.168.4.53: 27053 从
(2) 每台主机安装MongoDB同时配置文件加上一行:replSet rs1
vim /usr/local/mongodb/etc/mongodb.conf logpath=/usr/local/mongodb/log/mongodb.log logappend=true dbpath=/usr/local/mongodb/data/db fork=true port=27051 bind_ip=192.168.4.51 replSet=rs1 #定义副本集名称为rs1
(3) 配置节点信息
任意一台主机上执行如下操作:其中,config仅为名称,可以自由定义。
config={ _id:"rs1", members:[ {_id:0,host:"192.168.4.51:27051"}, {_id:1,host:"192.168.4.52:27052"}, {_id:2,host:"192.168.4.53:27053"} ] }
(4) 初始化Replica Set环境
rs.initiate(config)
(5) 查看副本信息
- 查看状态信息: rs.status()
- 查看是否为master库: rs.isMaster()
- 同步数据验证,允许从库查看数据(从库默认只同步数据,不能操作数据): db.getMongo().setSlaveOk()
二. 文档管理
1. 插入文档 save insert insertMany
- save: _id存在,则修改文档字段值,_id不存在,插入文档
- insert: _id存在,放弃插入,_id不存在,插入文档
#插入多行时,insert和insertMany都可以 db.aaa.insert([{name:"aaa",home:"beijing"},{name:"bbb",home:'shenzhen'}]) db.aaa.insertMany([{name:"newa",home:"beijing"},{name:"newb",home:'shenzhen'}])
2. 查询文档 fine findOne
- 行数显示限制:
- limit(数字) //显示前几行
- skip(数字) //跳过前几行
- sort({字段名}) //1升序,-1降序
db.aaa.find(); db.passwd.find({shell:"/sbin/nologin"},{_id:0,name:1,uid:1}).limit(5) #显示前5行 db.passwd.find({shell:"/sbin/nologin"},{_id:0,name:1,uid:1}).skip(2).limit(5) #跳过前2行,再显示5行 db.passwd.find({shell:"/sbin/nologin"},{_id:0,name:1,uid:1}).sort({uid:1}) #uid升序排列 db.passwd.find({shell:"/sbin/nologin"},{_id:0,name:1,uid:1}).sort({uid:-1}) #uid降序排列
- 范围比较:
- 相等 {字段名:值}
- 逻辑与 {字段名:值,字段名:值}
- 逻辑或 在....里 {字段名:{$in:[值1,值2,........值n]}}
- 逻辑或 不在....里 {字段名:{$nin:[值1,值2,........值n]}}
- 逻辑或 或者 {$or:[{字段名:值},{字段名:值},{字段名:值}.....]}
db.passwd.find({name:"root",uid:0},{_id:0,name:1,uid:1}) #逻辑与,满足两个条件 db.passwd.find({name:{$in:["mail","root","xxx"]}},{name:1,_id:0}) #逻辑或,三个条件满足几个显示几个 db.passwd.find({name:{$nin:["root","xxx"]}},{name:1,_id:0}) #逻辑或 db.passwd.find({$or:[{uid:1},{uid:8},{name:"root"}]},{name:1,uid:1,_id:0})
- 正则匹配
db.passwd.find({name:/^a/ },{_id:0,name:1,uid:1}) #匹配名字a开头 db.passwd.find({name:/p$/ },{_id:0,name:1,uid:1}) #匹配名字p结尾
- 数值比较
- $lt(<) $lte(<=) $gt(>) $gte(>=) $ne(!=)
db.passwd.find({ uid:{$lt:5} },{_id:0,name:1,uid:1}) #uid<5 db.passwd.find({ uid:{$gt:1000} },{_id:0,name:1,uid:1}) #uid>1000 db.passwd.find({ uid:{$gte:100,$lte:200} },{_id:0,name:1,uid:1}) #100<=uid号<=200 db.passwd.find({ uid:{$gte:0,$lte:5,$ne:3} },{_id:0,name:1,uid:1}) #0<=uid号<=5且!=3
- null匹配
db.passwd.save({name:null,uid:null}) db.passwd.find({name:null})
3. 更新文档
(1) update({条件},{修改的字段})
【注】此格式只修改匹配文档的第一行,且只留下修改的字段,其他字段均删除。
db.passwd.find({uid:{$lte:3}},{_id:0,name:1,uid:1,password:1}) #输出 { "name" : "root", "password" : "x", "uid" : 0 } { "name" : "bin", "password" : "x", "uid" : 1 } { "name" : "daemon", "password" : "x", "uid" : 2 } { "name" : "adm", "password" : "x", "uid" : 3 } db.passwd.update({uid:{$lte:3}},{password:"888"}) #更新 WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) db.passwd.find({uid:{$lte:3}},{_id:0,name:1,uid:1,password:1}) #再次查询,输出 { "name" : "bin", "password" : "x", "uid" : 1 } { "name" : "daemon", "password" : "x", "uid" : 2 } { "name" : "adm", "password" : "x", "uid" : 3 } db.passwd.find({password:"888"}) #修改的输出 { "_id" : ObjectId("5b861362798eac5b9bacbb9b"), "password" : "888" }
(2) update({条件},{$set:{修改的字段} })
【注】此格式只修改匹配文档的第一行,且只修改匹配的字段,其他字段不变。
db.passwd.find({uid:{$lte:3}},{_id:0,name:1,uid:1,password:1}) #查询 { "name" : "bin", "password" : "x", "uid" : 1 } { "name" : "daemon", "password" : "x", "uid" : 2 } { "name" : "adm", "password" : "x", "uid" : 3 } db.passwd.update({uid:{$lte:3}},{$set:{password:"999"}}) #更新 WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) db.passwd.find({uid:{$lte:3}},{_id:0,name:1,uid:1,password:1}) #再次查询 { "name" : "bin", "password" : "999", "uid" : 1 } #只更新了这一行 { "name" : "daemon", "password" : "x", "uid" : 2 } { "name" : "adm", "password" : "x", "uid" : 3 }
(3) update({条件},{$set:{修改的字段} }, false, true)
【注】此格式修改所有的匹配文档,且只修改匹配的字段,其他字段不变。
db.passwd.find({uid:{$lte:3}},{_id:0,name:1,uid:1,password:1}) #查询 { "name" : "bin", "password" : "999", "uid" : 1 } { "name" : "daemon", "password" : "x", "uid" : 2 } { "name" : "adm", "password" : "x", "uid" : 3 } db.passwd.update({uid:{$lte:3}},{$set:{password:"FFF",gid:7}},false,true) #更新所有文档 WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 3 }) db.passwd.find({uid:{$lte:3}},{_id:0,name:1,uid:1,password:1,gid:1}) #更新后再次查询 { "name" : "bin", "password" : "FFF", "uid" : 1, "gid" : 7 } #所有行都更新了 { "name" : "daemon", "password" : "FFF", "uid" : 2, "gid" : 7 } { "name" : "adm", "password" : "FFF", "uid" : 3, "gid" : 7 }
(4) 删除与条件匹配的文档字段 update({条件},{$unset:{修改的字段} })
db.passwd.find({uid:{$lte:3}},{_id:0,name:1,uid:1,password:1,gid:1}) #查询 { "name" : "bin", "password" : "FFF", "uid" : 1, "gid" : 7 } { "name" : "daemon", "password" : "FFF", "uid" : 2, "gid" : 7 } { "name" : "adm", "password" : "FFF", "uid" : 3, "gid" : 7 } db.passwd.update({uid:{$lte:3}},{$unset:{gid:7}}) #删除gid为7的列 WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) db.passwd.find({uid:{$lte:3}},{_id:0,name:1,uid:1,password:1,gid:1}) #删除后查询 { "name" : "bin", "password" : "FFF", "uid" : 1 } #只删除了第一个匹配的文档的列 { "name" : "daemon", "password" : "FFF", "uid" : 2, "gid" : 7 } { "name" : "adm", "password" : "FFF", "uid" : 3, "gid" : 7 } db.passwd.update({uid:{$lte:3}},{$unset:{gid:7}},false,true) #加上false,true参数 WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 2 }) db.passwd.find({uid:{$lte:3}},{_id:0,name:1,uid:1,password:1,gid:1}) #再次查询 { "name" : "bin", "password" : "FFF", "uid" : 1 } #删除了所有匹配文档的列 { "name" : "daemon", "password" : "FFF", "uid" : 2 } { "name" : "adm", "password" : "FFF", "uid" : 3 }
(5) 字段值自加或自减 update({条件},{$inc:{字段名:数字} })
db.passwd.update({ uid:{$lte:5} },{ $inc:{uid:1} }) #uid自加1,只更新第一行 db.passwd.update({ uid:{$lte:5} },{$inc:{uid:1}} , false, true ) #uid自加1,更新所有行 db.passwd.update({ uid:{$lte:5} },{$inc:{uid:-2}}, false, true ) #uid自减2,更新所有行
(6) 更新数组 $push $addToSet $pop $pull
db.aaa.insert({name:"tom",hobby:["sing","tennis","movie"]}) #插入 db.aaa.find({name:"tom"},{_id:0,name:1,hobby:1}) #输出 { "name" : "tom", "hobby" : [ "sing", "tennis", "movie" ] } db.aaa.update({name:"tom"},{$push:{hobby:"dance"}}) #数组插入push db.aaa.update({name:"tom"},{$push:{hobby:"dance"}}) #push可插入相同值 db.aaa.find({name:"tom"},{_id:0,name:1,hobby:1}) #输出 { "name" : "tom", "hobby" : [ "sing", "tennis", "movie", "dance", "dance" ] } db.aaa.update({name:"tom"},{$addToSet:{hobby:"dance"}}) #数组插入addToSet db.aaa.find({name:"tom"},{_id:0,name:1,hobby:1}) #查询可知,重复值插入不成功 { "name" : "tom", "hobby" : [ "sing", "tennis", "movie", "dance", "dance" ] } db.aaa.update({name:"tom"},{$addToSet:{hobby:"game"}}) #插入非重复值 db.aaa.find({name:"tom"},{_id:0,name:1,hobby:1}) #输出,插入成功 { "name" : "tom", "hobby" : [ "sing", "tennis", "movie", "dance", "dance", "game" ] } db.aaa.update({name:"tom"},{$pop:{hobby:1}}) #删除数组尾部元素pop 1 db.aaa.find({name:"tom"},{_id:0,name:1,hobby:1}) #输出 { "name" : "tom", "hobby" : [ "sing", "tennis", "movie", "dance", "dance" ] } db.aaa.update({name:"tom"},{$pop:{hobby:-1}}) #删除数组头部元素 pop -1 db.aaa.find({name:"tom"},{_id:0,name:1,hobby:1}) #输出 { "name" : "tom", "hobby" : [ "tennis", "movie", "dance", "dance" ] } db.aaa.update({name:"tom"},{$pull:{hobby:"dance"}}) #删除数组某个元素pull db.aaa.find({name:"tom"},{_id:0,name:1,hobby:1}) #输出 { "name" : "tom", "hobby" : [ "tennis", "movie" ] }
4. 删除文档
- 删除集合同时删除索引:$drop()
- 删除文档不删除索引:$remove()
db.bbb.drop() #删除bbb集合 db.passwd.remove({name:"ftp"}) #条件删除文档 db.passwd.remove({}) #删除所有文档
相关推荐
jaylong 2020-08-19
shining0 2020-08-02
lbyd0 2020-11-17
BigYellow 2020-11-16
sushuanglei 2020-11-12
我心似明月 2020-11-09
zhushenghan 2020-11-09
sunnnyduan 2020-10-16
不要皱眉 2020-10-14
xiaohai 2020-09-29
songxiugongwang 2020-09-22
萌亖 2020-09-17
LuckyLXG 2020-09-08
sdmzhu 2020-09-01
mkhhxxttxs 2020-09-16
xiaohai 2020-09-16
newcome 2020-09-09
大秦铁骑 2020-08-19