我正在使用mongodb3.6和nodejs mongo驱动程序3.0。以下是我的源代码:
const col = client.db('test').collection('test');
col
.insert({a: false}, {w: 0, j: false})
.then(() => {
return col.count({}, {readPreference: ReadPreference.SECONDARY});
})
.then(ret => {
console.log(ret);
}).catch(err => console.error(err));
程序挂起col.count
命令。如果我删除readPreference
选项,它可以正常工作。我的副本集读取首选项设置为primaryPrefered
,如下所示:
rs1:PRIMARY> db.getMongo().getReadPref()
{ "mode" : "primaryPrefered" }
我不知道为什么secondary
读取首选项会阻止我的应用程序。如何在我的应用程序中正确使用它?
以下是我的副本设置状态:
rs1:PRIMARY> rs.status()
{
"set" : "rs1",
"date" : ISODate("2018-04-13T01:06:49.269Z"),
"myState" : 1,
"term" : NumberLong(22),
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1523581602, 1),
"t" : NumberLong(22)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1523581602, 1),
"t" : NumberLong(22)
},
"appliedOpTime" : {
"ts" : Timestamp(1523581602, 1),
"t" : NumberLong(22)
},
"durableOpTime" : {
"ts" : Timestamp(1523581602, 1),
"t" : NumberLong(22)
}
},
"members" : [
{
"_id" : 0,
"name" : "mongo1:37017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 155084,
"optime" : {
"ts" : Timestamp(1523581602, 1),
"t" : NumberLong(22)
},
"optimeDate" : ISODate("2018-04-13T01:06:42Z"),
"electionTime" : Timestamp(1523537159, 1),
"electionDate" : ISODate("2018-04-12T12:45:59Z"),
"configVersion" : 199516,
"self" : true
},
{
"_id" : 1,
"name" : "mongo2:37018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 44550,
"optime" : {
"ts" : Timestamp(1523581602, 1),
"t" : NumberLong(22)
},
"optimeDurable" : {
"ts" : Timestamp(1523581602, 1),
"t" : NumberLong(22)
},
"optimeDate" : ISODate("2018-04-13T01:06:42Z"),
"optimeDurableDate" : ISODate("2018-04-13T01:06:42Z"),
"lastHeartbeat" : ISODate("2018-04-13T01:06:48.757Z"),
"lastHeartbeatRecv" : ISODate("2018-04-13T01:06:48.756Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "mongo1:37017",
"configVersion" : 199516
},
{
"_id" : 2,
"name" : "mongo3:37019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 44480,
"optime" : {
"ts" : Timestamp(1523581602, 1),
"t" : NumberLong(22)
},
"optimeDurable" : {
"ts" : Timestamp(1523581602, 1),
"t" : NumberLong(22)
},
"optimeDate" : ISODate("2018-04-13T01:06:42Z"),
"optimeDurableDate" : ISODate("2018-04-13T01:06:42Z"),
"lastHeartbeat" : ISODate("2018-04-13T01:06:48.758Z"),
"lastHeartbeatRecv" : ISODate("2018-04-13T01:06:48.757Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "mongo2:37018",
"configVersion" : 199516
}
],
"ok" : 1,
"operationTime" : Timestamp(1523581602, 1)
}