配置MongoDB副本集

Posted IT资料群1085043784

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了配置MongoDB副本集相关的知识,希望对你有一定的参考价值。

1 案例1:配置MongoDB副本集

1.1 问题

  • 具体要求:
  • 准备3台mongodb服务器
  • 配置副本集服务
  • 验证副本集配置

1.2 方案

准备三台虚拟机,配置mongodb副本集,ip分别为192.158.4.51, 192.168.4.52,192.168.4.53其中一个是主节点,负责处理客户端请求,其余的都是从节点,负责复制主节点上的数据,实现存储数据副本,提高了数据的可用性,具体分配如表-1所示:

表-1

技术图片

1.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:创建mongodb副本集

1)三台主机安装mongodb(以4.51为例)

  1. [[email protected] ~]# cd mongodb/
  2. [[email protected] mongodb]# mkdir /usr/local/mongodb
  3. [[email protected] mongodb]# cd /usr/local/mongodb
  4. [[email protected] mongodb]# cp -r
  5. /root/mongodb/mongodb-linux-x86_64-rhel70-3.6.3/bin/ .
  6. [[email protected] mongodb]# ls
  7. bin
  8. [[email protected] mongodb]# mkdir etc
  9. [[email protected] mongodb]# mkdir log
  10. [[email protected] mongodb]# mkdir -p data/db
  11. [[email protected] mongodb]# vim etc/mongodb.conf
  12. dbpath=/usr/local/mongodb/data/db/
  13. logpath=/usr/local/mongodb/log/mongodb.log
  14. logappend=true
  15. fork=true
  16. bind_ip=192.168.4.51
  17. port=27077
  18. replSet=rs1        
  19. //加入到副本集,rs1名字随便起,想知道谁和我在一个副本集里,三台机器的名字一样,都写rs1

2)设置PATH变量

  1. [[email protected] mongodb]# vim /etc/profile
  2. export PATH=/usr/local/mongodb/bin:$PATH
  3. [[email protected] mongodb]# source /etc/profile

3)由于启动和停止服务名字太长,可以起一个别名

给停止服务起一个别名

  1. [[email protected] mongodb]# alias cmdb=‘mongod --dbpath=/usr/local/mongodb/data/db/ --shutdown‘

给启动服务起一个别名

  1. [[email protected] mongodb]# alias smdb=‘mongod -f /usr/local/mongodb/etc/mongodb.conf‘

4)启动服务并连接

  1. [[email protected] ~]# smdb
  2. about to fork child process, waiting until server is ready for connections.
  3. forked process: 5656
  4. child process started successfully, parent exiting
  5. [[email protected] ~]# mongo --host 192.168.4.51 --port 27077
  6. MongoDB shell version v3.6.3
  7. connecting to: mongodb://192.168.4.51:27077/
  8. MongoDB server version: 3.6.3
  9. ...
  10. >

5)配置集群信息,任意一台都可以,在这里在51上面操作

  1. > rs1_config = {        //rs1_config随便起变量名,要记住
  2. _id:"rs1",         //必须为rs1这个,三台主机集群名,配置文件里面写的是这个
  3. members:[
  4. {_id:0,host:"192.168.4.51:27077"},        //_id值随意,host值固定
  5. {_id:1,host:"192.168.4.52:27078"},
  6. {_id:2,host:"192.168.4.53:27079"}
  7. ]
  8. };        //回车,出现下面情况为成功
  9. {
  10.     "_id" : "rs1",
  11.     "members" : [
  12.         {
  13.             "_id" : 0,
  14.             "host" : "192.168.4.51:27077"
  15.         },
  16.         {
  17.             "_id" : 1,
  18.             "host" : "192.168.4.52:27078"
  19.         },
  20.         {
  21.             "_id" : 2,
  22.             "host" : "192.168.4.53:27079"
  23.         }
  24.     ]
  25. }
  26. >

6)初始化Replica Sets环境

  1. > rs.initiate(rs1_config)
  2. {
  3.     "ok" : 1,
  4.     "operationTime" : Timestamp(1538187475, 1),
  5.     "$clusterTime" : {
  6.         "clusterTime" : Timestamp(1538187475, 1),
  7.         "signature" : {
  8.             "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
  9.             "keyId" : NumberLong(0)
  10.         }
  11.     }
  12. }
  13. rs1:SECONDARY>
  14. rs1:PRIMARY>            //提示PRIMARY,51为主

7)在52和53上面查看

  1. [[email protected] ~]# mongo --host 192.168.4.52 --port 27078
  2. MongoDB shell version v3.6.3
  3. connecting to: mongodb://192.168.4.52:27078/
  4. MongoDB server version: 3.6.3
  5. ...
  6. ...
  7. rs1:SECONDARY>         //提示SECONDARY,52为从
  8. rs1:SECONDARY>
  9. rs1:SECONDARY>
  10. [[email protected]192 ~]# mongo --host 192.168.4.53 --port 27079
  11. MongoDB shell version v3.6.3
  12. connecting to: mongodb://192.168.4.53:27079/
  13. MongoDB server version: 3.6.3
  14. ...
  15. ...
  16. rs1:SECONDARY>     //提示SECONDARY,53为从
  17. rs1:SECONDARY>

注意:如果初始化错误,重启服务登陆之后重新设置变量,之后再重新初始化

8)查看状态信息

  1. rs1:PRIMARY> rs.status()
  2. ...
  3.     "members" : [
  4.         {
  5.             "_id" : 0,
  6.             "name" : "192.168.4.51:27077",
  7.             "health" : 1,
  8.             "state" : 1,
  9.             "stateStr" : "PRIMARY",
  10.             "uptime" : 2295,
  11.             ...
  12. ...
  13.         },
  14.         {
  15.             "_id" : 1,
  16.             "name" : "192.168.4.52:27078",
  17.             "health" : 1,
  18.             "state" : 2,
  19.             "stateStr" : "SECONDARY",
  20.             "uptime" : 384,
  21.             ...
  22. ...
  23.         },
  24.         {
  25.             "_id" : 2,
  26.             "name" : "192.168.4.53:27079",
  27.             "health" : 1,
  28.             "state" : 2,
  29.             "stateStr" : "SECONDARY",
  30. ...
  31. ...

9)查看是否是master库

  1. rs1:PRIMARY> rs .isMaster( )
  2. {
  3.     "hosts" : [
  4.         "192.168.4.51:27077",
  5.         "192.168.4.52:27078",
  6.         "192.168.4.53:27079"
  7.     ],
  8.     "setName" : "rs1",
  9.     "setVersion" : 1,
  10.     "ismaster" : true,        //主库
  11.     "secondary" : false,
  12.     "primary" : "192.168.4.51:27077",
  13.     "me" : "192.168.4.51:27077",
  14. ...
  15. ...

10)验证副本集,同步数据验证(51上面写数据)

  1. rs1:PRIMARY> use gamedb2
  2. switched to db gamedb2
  3. rs1:PRIMARY> db.a.save({name:"yaya",age:75,em:"[email protected]"})
  4. WriteResult({ "nInserted" : 1 })

52上面查看

  1. [[email protected] ~]# mongo --host 192.168.4.52 --port 27078
  2. rs1:SECONDARY> db.getMongo().setSlaveOk()
  3. rs1:SECONDARY> show dbs        //有gamedb2库
  4. admin 0.000GB
  5. config 0.000GB
  6. ddsdb 0.000GB
  7. gamedb2 0.000GB
  8. local 0.000GB
  9. test 0.000GB

步骤三:切换主库验证

1)自动切换主库验证

关闭51

  1. [[email protected] ~]# cmdb        //之前设置的别名
  2. killing process with pid: 5656

查看52和53

  1. [[email protected] ~]# mongo --host 192.168.4.52 --port 27078
  2. MongoDB shell version v3.6.3
  3. connecting to: mongodb://192.168.4.52:27078/
  4. MongoDB server version: 3.6.3
  5. ...
  6. ...
  7. rs1:PRIMARY>         //52为主
  8. rs1:PRIMARY>
  9. [[email protected] ~]# mongo --host 192.168.4.53 --port 27079
  10. MongoDB shell version v3.6.3
  11. connecting to: mongodb://192.168.4.53:27079/
  12. MongoDB server version: 3.6.3
  13. ...
  14. ...
  15. rs1:SECONDARY>        //53为从

启动51,启动后不会再变为主,会成为52的从

  1. [[email protected] ~]# smdb
  2. about to fork child process, waiting until server is ready for connections.
  3. forked process: 6598
  4. child process started successfully, parent exiting
  5. rs1:SECONDARY> rs.isMaster()
  6. {
  7.     "hosts" : [
  8.         "192.168.4.51:27077",
  9.         "192.168.4.52:27078",
  10.         "192.168.4.53:27079"
  11.     ],
  12.     "setName" : "rs1",
  13.     "setVersion" : 1,
  14.     "ismaster" : false,
  15.     "secondary" : true,
  16.     "primary" : "192.168.4.52:27078",
  17.     "me" : "192.168.4.51:27077",
  18. ...

2 案例2:文档管理

2.1 问题

  • 基于MongoDB环境完成下列练习:
  • 插入文档
  • 查询文档
  • 更新文档
  • 删除文档

2.2 步骤

实现此案例需要按照如下步骤进行。

步骤一:管理文档

1)把系统用户信息/etc/passwd存储到mdb库下的user集合里

  1. rs1:PRIMARY> use mdb
  2. switched to db mdb
  3. rs1:PRIMARY> db.user.save({name:"yaya",password:"x",uid:9999,gid:9999,comment:"",homdir:"/home",shell:"/bin/bash"})
  4. WriteResult({ "nInserted" : 1 })
  5. rs1:PRIMARY> exit
  6. bye
  7. [[email protected] ~]# mongoexport --host 192.168.4.52 --port 27078 -d mdb -c user -f name,password,uid,gid,comment,homdir,shell --type=csv -o /tmp/user.csv
  8. 2018-09-29T11:04:14.967+0800    connected to: 192.168.4.52:27078
  9. 2018-09-29T11:04:14.968+0800    exported 1 record
  10. [[email protected] ~]# cp /etc/passwd /tmp
  11. [[email protected] ~]# sed -i ‘s/:/,/g‘ /tmp/passwd
  12. [[email protected] ~]# sed -i ‘$r /tmp/passwd‘ /tmp/user.csv
  13. [[email protected] ~]# mongoimport --host 192.168.4.52 --port 27078 -d mdb -c user --headerline --type=csv /tmp/user.csv
  14. 2018-09-29T11:06:08.355+0800    connected to: 192.168.4.52:27078
  15. 2018-09-29T11:06:08.363+0800    imported 41 documents

2)查看

  1. [[email protected] ~]# mongo --host 192.168.4.52 --port 27078
  2. rs1:PRIMARY> use mdb
  3. switched to db mdb
  4. rs1:PRIMARY> db.user.findOne()
  5. {
  6.     "_id" : ObjectId("5baeeb37ce3cc5539aa21f38"),
  7.     "name" : "yaya",
  8.     "password" : "x",
  9.     "uid" : 9999,
  10.     "gid" : 9999,
  11.     "comment" : "",
  12.     "homdir" : "/home",
  13.     "shell" : "/bin/bash"
  14. }

db.user.find({条件},{定义显示的字段}) #指定查询条件并指定显示的字段

  1. rs1:PRIMARY> db.user.find()
  2. { "_id" : ObjectId("5baeeb37ce3cc5539aa21f38"), "name" : "yaya", "password" : "x", "uid" : 9999, "gid" : 9999, "comment" : "", "homdir" : "/home", "shell" : "/bin/bash" }
  3. ...
  4. Type "it" for more            //出现这个按it,默认出现20行

查看每行的name字段

  1. rs1:PRIMARY> db.user.find({},{name:1})        
  2. { "_id" : ObjectId("5baeeb37ce3cc5539aa21f38"), "name" : "yaya" }
  3. ...
  4. ...
  5. { "_id" : ObjectId("5baeec2001805180a1011843"), "name" : "rpc" }
  6. Type "it" for more

不看_id字段

  1. rs1:PRIMARY> db.user.find({},{_id:0})
  2. { "name" : "yaya", "password" : "x", "uid" : 9999, "gid" : 9999, "comment" : "", "homdir" : "/home", "shell" : "/bin/bash" }
  3. ...
  4. ...
  5. { "name" : "rpc", "password" : "x", "uid" : 32, "gid" : 32, "comment" : "Rpcbind Daemon", "homdir" : "/var/lib/rpcbind", "shell" : "/sbin/nologin" }
  6. Type "it" for more

不看_id那一列,看name那一列

  1. rs1:PRIMARY> db.user.find({},{_id:0,name:1})
  2. { "name" : "yaya" }
  3. ...
  4. ...
  5. { "name" : "rpc" }
  6. Type "it" for more

查看以a开头的name字段

  1. rs1:PRIMARY> db.user.find({name:/^a/},{_id:0})
  2. { "name" : "adm", "password" : "x", "uid" : 3, "gid" : 4, "comment" : "adm", "homdir" : "/var/adm", "shell" : "/sbin/nologin" }
  3. { "name" : "abrt", "password" : "x", "uid" : 173, "gid" : 173, "comment" : "", "homdir" : "/etc/abrt", "shell" : "/sbin/nologin" }
  4. { "name" : "avahi", "password" : "x", "uid" : 70, "gid" : 70, "comment" : "Avahi mDNS/DNS-SD Stack", "homdir" : "/var/run/avahi-daemon", "shell" : "/sbin/nologin" }

显示查询结果的前一行

limit 数字

  1. rs1:PRIMARY> db.user.find({name:/^a/},{_id:0}).limit (1)
  2. { "name" : "adm", "password" : "x", "uid" : 3, "gid" : 4, "comment" : "adm", "homdir" : "/var/adm", "shell" : "/sbin/nologin" }

显示name字段以a开头的第一行

  1. rs1:PRIMARY> db.user.findOne({name:/^a/},{_id:0,name:1,shell:1,uid:1})
  2. { "name" : "adm", "uid" : 3, "shell" : "/sbin/nologin" }

跳过几行显示 (2行)

skip 数字

  1. rs1:PRIMARY> db.user.find({name:/^a/},{_id:0,name:1,shell:1}).skip (2)
  2. { "name" : "avahi", "shell" : "/sbin/nologin" }

默认升序排序

sort 字段名

  1. rs1:PRIMARY> db.user.find({name:/^a/},{_id:0,name:1,shell:1,uid:1}).sort({uid:1})
  2. { "name" : "adm", "uid" : 3, "shell" : "/sbin/nologin" }
  3. { "name" : "avahi", "uid" : 70, "shell" : "/sbin/nologin" }
  4. { "name" : "abrt", "uid" : 173, "shell" : "/sbin/nologin" }

降序排序

  1. rs1:PRIMARY> db.user.find({name:/^a/},{_id:0,name:1,shell:1,uid:1}).sort({uid:-1})
  2. { "name" : "abrt", "uid" : 173, "shell" : "/sbin/nologin" }
  3. { "name" : "avahi", "uid" : 70, "shell" : "/sbin/nologin" }
  4. { "name" : "adm", "uid" : 3, "shell" : "/sbin/nologin" }

显示name字段以a开头和uid为3的所有行

  1. rs1:PRIMARY> db.user.find({name:/^a/,uid:3},{_id:0,name:1,shell:1,uid:1})
  2. { "name" : "adm", "uid" : 3, "shell" : "/sbin/nologin" }

3)条件判断的表示方式

$in 在...里

  1. rs1:PRIMARY> db.user.find({uid:{$in:[1,6,9]}})        //uid的为1或者6或者9的匹配
  2. { "_id" : ObjectId("5baeec2001805180a1011833"), "name" : "bin", "password" : "x", "uid" : 1, "gid" : 1, "comment" : "bin", "homdir" : "/bin", "shell" : "/sbin/nologin" }
  3. { "_id" : ObjectId("5baeec2001805180a1011838"), "name" : "shutdown", "password" : "x", "uid" : 6, "gid" : 0, "comment" : "shutdown", "homdir" : "/sbin", "shell" : "/sbin/shutdown" }

$nin 不在...里

  1. rs1:PRIMARY> db.user.find({uid:{$nin:[1,6,9]}},{_id:0,name:1,uid:1})
  2. { "name" : "yaya", "uid" : 9999 }
  3. ...
  4. ...
  5. { "name" : "saslauth", "uid" : 996 }
  6. Type "it" for more

$or 条件满足任意其中一个即可

  1. rs1:PRIMARY> db.user.find({$or:[{name:"root"},{uid:1}]},{_id:0,name:1,uid:1})
  2. { "name" : "root", "uid" : 0 }
  3. { "name" : "bin", "uid" : 1 }

4)正则匹配,以a开头的name字段

  1. rs1:PRIMARY> db.user.find({name:/^a/},{_id:0,name:1,uid:1})
  2. { "name" : "adm", "uid" : 3 }
  3. { "name" : "abrt", "uid" : 173 }
  4. { "name" : "avahi", "uid" : 70 }

5)数值比较

$lt(小于) $lte(小于等于) $gt(大于) $gte(大于等于) $ne(不等于)

  1. rs1:PRIMARY> db.user.find({uid:{$gte:10,$lte:40}},{_id:0,name:1,uid:1})
  2. { "name" : "operator", "uid" : 11 }
  3. { "name" : "games", "uid" : 12 }
  4. { "name" : "ftp", "uid" : 14 }
  5. { "name" : "rpc", "uid" : 32 }
  6. { "name" : "rpcuser", "uid" : 29 }
  7. { "name" : "ntp", "uid" : 38 }

匹配null:可以匹配没有的字段,也可以检查这个字段有没有

  1. rs1:PRIMARY> db.user.save({name:null,uid:null})
  2. WriteResult({ "nInserted" : 1 })
  3. rs1:PRIMARY> db.user.find({name:null})
  4. { "_id" : ObjectId("5baef385f9f3bf625ea1dbd6"), "name" : null, "uid" : null }
  5. rs1:PRIMARY> db.user.find({shell:null})    //表示此条文档没有shell字段
  6. { "_id" : ObjectId("5baef385f9f3bf625ea1dbd6"), "name" : null, "uid" : null }
  7. rs1:PRIMARY>

6)save和insert的区别

相同点:当集合不存在时创建集合,并插入记录

不同点:save() _id字段值已经存在时,修改文档字段值

insert() _id字段值已经存在时,放弃修改文档字段值

  1. rs1:PRIMARY> db.t1.save({name:"bob",age:19})
  2. WriteResult({ "nInserted" : 1 })
  3. rs1:PRIMARY> db.t1.insert({name:"bob",age:19})
  4. WriteResult({ "nInserted" : 1 })
  5. rs1:PRIMARY> db.t1.save({_id:7,name:"bob",age:19})
  6. WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : 7 })
  7. rs1:PRIMARY> db.t1.find()
  8. ...
  9. ...
  10. { "_id" : 7, "name" : "bob", "age" : 19 }
  11. rs1:PRIMARY> db.t1.save({_id:7,name:"tom",age:19})        //把上一条的记录直接修改
  12. WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
  13. rs1:PRIMARY> db.t1.find()
  14. ...
  15. ...
  16. { "_id" : 7, "name" : "tom", "age" : 19 }
  17. rs1:PRIMARY>
  18. rs1:PRIMARY>
  19. rs1:PRIMARY> db.t1.insert({_id:8,name:"tom",age:19})        //可以存上
  20. WriteResult({ "nInserted" : 1 })
  21. rs1:PRIMARY> db.t1.insert({_id:8,name:"tom",age:19})        //存不上
  22. WriteResult({
  23.     "nInserted" : 0,
  24.     "writeError" : {
  25.         "code" : 11000,
  26.         "errmsg" : "E11000 duplicate key error collection: mdb.t1 index: _id_ dup key: { : 8.0 }"
  27.     }
  28. })

7)插入多行文档

  1. rs1:PRIMARY> db.t1.insertMany([{name:"xiaojiu"},{name:"laoshi"}])
  2. {
  3.     "acknowledged" : true,
  4.     "insertedIds" : [
  5.         ObjectId("5baef526f9f3bf625ea1dbd9"),
  6.         ObjectId("5baef526f9f3bf625ea1dbda")
  7.     ]
  8. }
  9. rs1:PRIMARY> db.t1.find()
  10. ...
  11. ...
  12. { "_id" : ObjectId("5baef526f9f3bf625ea1dbd9"), "name" : "xiaojiu" }
  13. { "_id" : ObjectId("5baef526f9f3bf625ea1dbda"), "name" : "laoshi" }

8)update修改

  1. rs1:PRIMARY> db.user.update({name:"root"},{password:"XXX"})
  2. //如果这一列不写完整,这一行除了password这一行,这一列的其他值都没有了相当于删除(要写完整)
  3. WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
  4. rs1:PRIMARY> db.t1.find({name:"root"})
  5. rs1:PRIMARY> db.user.find({name:"root"}) //没有东西,除了password:"XXX"

9)$set 条件匹配时,修改指定字段的值(局部修改)

  1. rs1:PRIMARY> db.user.update({name:"adm"},{$set:{password:"AAA"}})
  2. WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
  3. rs1:PRIMARY> db.user.find({name:"adm"})        //还存在
  4. { "_id" : ObjectId("5baeec2001805180a1011835"), "name" : "adm", "password" : "AAA", "uid" : 3, "gid" : 4, "comment" : "adm", "homdir" : "/var/adm", "shell" : "/sbin/nologin" }
  5. rs1:PRIMARY> db.user.update({name:/^r/},{$set:{password:"FFF"}})        
  6. //默认修改匹配条件的第一行
  7. WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })    
  8. rs1:PRIMARY> db.user.update({name:/^a/},{$set:{password:"FFF"}},false,true)
  9. //改匹配到的所有
  10. WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 3 })

10)$unset 删除与条件匹配文档的字段

  1. rs1:PRIMARY> db.user.update({name:"sync"},{$unset:{password:1}})     
  2. //删除password字段
  3. WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

11)数组

  1. rs1:PRIMARY> db.user.insert({name:"bob",like:["a","b","c","d","e","f",]})
  2. WriteResult({ "nInserted" : 1 })

$pop 删除数组末尾一个元素,1删除最后一个,-1删除第一个

  1. rs1:PRIMARY> db.user.update({name:"bob"},{$pop:{like:1}})    
  2. //删除匹配的第一条的最后一个
  3. WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
  4. rs1:PRIMARY> db.user.update({name:"bob"},{$pop:{like:-1}})    
  5. //删除匹配的第一条的第一个
  6. WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

$push 向数组中添加新元素

  1. rs1:PRIMARY> db.user.update({name:"bob"},{$push:{like:"Z"}})        //默认添加到最后
  2. WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
  3. rs1:PRIMARY> db.user.update({name:"bob"},{$push:{like:"W"}})
  4. WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
  5. rs1:PRIMARY> db.user.find({name:"bob"})
  6. { "_id" : ObjectId("5baef7b2034891a205de2959"), "name" : "bob", "like" : [ "b", "c", "d", "e", "Z", "W" ] }

$addToSet 避免重复添加

  1. rs1:PRIMARY> db.user.update({name:"bob"},{$addToSet:{like:"W"}})
  2. WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })
  3. rs1:PRIMARY> db.user.find({name:"bob"})
  4. { "_id" : ObjectId("5baef7b2034891a205de2959"), "name" : "bob", "like" : [ "b", "c", "d", "e", "Z", "W" ] }

$pull 删除数组里的指定元素,若有两个bob可以用_id值定义把name:"bob"换成id值

  1. rs1:PRIMARY> db.user.update({name:"bob"},{$pull:{like:"c"}})
  2. WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
  3. rs1:PRIMARY> db.user.find({name:"bob"})
  4. { "_id" : ObjectId("5baef7b2034891a205de2959"), "name" : "bob", "like" : [ "b", "d", "e", "Z", "W" ] }
  5. rs1:PRIMARY> db.user.update({"_id":ObjectId("5afc1a717eff45e9cfc57ed3")},{$push:{like:"S"}})
  6. WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0 })
  7. rs1:PRIMARY>

$inc 条件匹配时,字段值自加或自减

  1. rs1:PRIMARY> db.user.update({uid:{$lte:10}},{$inc:{uid:2}})         
  2. //设置字段值自加2,默认改第一行
  3. WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
  4. rs1:PRIMARY> db.user.update({uid:{$lte:10}},{$inc:{uid:2}},false,true)    
  5. //设置字段值自加2,false,true改全部
  6. WriteResult({ "nMatched" : 8, "nUpserted" : 0, "nModified" : 8 })
  7. rs1:PRIMARY>
  8. rs1:PRIMARY> db.user.update({uid:{$lte:10}},{$inc:{uid:-1}})
  9. //负数时是自减1,默认改第一行
  10. WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

12)删除文档

remove()与drop()的区别

remove()删除文档时不删除索引

drop()删除集合的时候同时删除索引

  1. rs1:PRIMARY> db.t1.remove({})
  2. WriteResult({ "nRemoved" : 6 })
  3. rs1:PRIMARY> db.user.remove({name:"/^a/"})        //删除以a开头的记录
  4. WriteResult({ "nRemoved" : 0 })
  5. rs1:PRIMARY> db.t1.drop()        //删除集合t1
  6. true
  7. rs1:PRIMARY>

以上是关于配置MongoDB副本集的主要内容,如果未能解决你的问题,请参考以下文章

如何配置 MongoDB 副本集

[Database] MongoDB 副本集配置

mongodb带密码的副本集加入分片后怎么访问

搭建MongoDB的副本集

Mongodb3.0.5副本集搭建及spring和java连接副本集配置

window配置mongodb集群(副本集)