NoSQL——MongoDB副本集和文档管理

in 编程
关注公众号【好便宜】( ID:haopianyi222 ),领红包啦~
阿里云,国内最大的云服务商,注册就送数千元优惠券:https://t.cn/AiQe5A0g
腾讯云,良心云,价格优惠: https://t.cn/AieHwwKl
搬瓦工,CN2 GIA 优质线路,搭梯子、海外建站推荐: https://t.cn/AieHwfX9

一. 副本集

1. 介绍

2. MongoDB复制原理

3. 副本集实现方式

(1) 主从复制方式(宕机后需要手动配置,已不用)

(2) Replica Sets副本集

4. 配置Replica Sets

(1) 环境准备,3台主机

(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) 查看副本信息

二. 文档管理

1. 插入文档 save insert insertMany

#插入多行时,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

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降序排列
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结尾
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
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. 删除文档

db.bbb.drop()                    #删除bbb集合
db.passwd.remove({name:"ftp"})   #条件删除文档
db.passwd.remove({})             #删除所有文档

 

 

 

 

 

 

关注公众号【好便宜】( ID:haopianyi222 ),领红包啦~
阿里云,国内最大的云服务商,注册就送数千元优惠券:https://t.cn/AiQe5A0g
腾讯云,良心云,价格优惠: https://t.cn/AieHwwKl
搬瓦工,CN2 GIA 优质线路,搭梯子、海外建站推荐: https://t.cn/AieHwfX9
扫一扫关注公众号添加购物返利助手,领红包
Comments are closed.

推荐使用阿里云服务器

超多优惠券

服务器最低一折,一年不到100!

朕已阅去看看