Mongoose没有从Mongo辅助数据库中读取

时间:2015-07-12 16:13:01

标签: mongodb mongoose database-replication

我已经实现了一个我在全球范围内使用的副本集。我的主人在美国俄勒冈州和4个辅助人员。加州和弗吉尼亚,法兰克福和悉尼。我也在同一地区也有网络服务器。这些Web服务器使用mongoose连接到mongo:

var mongoose = require("mongoose");
var dbUrl = "mongodb://***.***.***.***:27017,***.***.***.***:27017,***.***.***.***:27017,***.***.***.***:27017,***.***.***.***:27017/exampleDb";
var dbOptions : {
   "replSet": {
      "rs_name": "exampleRepSet",
      "readPreference": "ReadPreference.SECONDARY_PREFERRED",
      "read_preference": "ReadPreference.SECONDARY_PREFERRED",
      "w":0,
      "slaveOk": true
    }
}
mongoose.connect(dbUrl, dbOptions);

我的问题是我的客户端具有更高的数据库延迟,具体取决于他们与主服务器的距离。加州获得40分钟,悉尼获得400分钟。我不明白为什么会这样,因为他们应该读取他们所在地区的二级数据库。

我知道必须对主要内容进行写入,但即使我执行了查找,也不应该在区域辅助中进行写入并快速返回?

我意识到该配置中有一些冗余选项,但我已经绝望了。我也尝试过选项"ReadPreference.NEAREST"无济于事。

3 个答案:

答案 0 :(得分:3)

修改

尝试使用mongodb://connection/db/?readPreference=secondary在连接字符串本身上设置读取首选项,而不是在dbOptions中。我在node-mongodb-native中找不到任何可以将读取首选项添加到replset配置中的内容。 http://mongodb.github.io/node-mongodb-native/2.0/api/ReplSet.html

旧答案

您可能需要将设置设置为最接近而不是辅助首选。 http://docs.mongodb.org/manual/reference/read-preference/#nearest

答案 1 :(得分:3)

尝试使用以下选项:

var mongoose = require("mongoose");
var dbUrl = "mongodb://***.***.***.***:27017,***.***.***.***:27017,***.***.***.***:27017,***.***.***.***:27017,***.***.***.***:27017/exampleDb";

mongoose.connect(dbUrl, {
    server: { 
        readPreference: "nearest", 
        strategy: "ping"
    },
    replset: { 
        rs_name: "exampleRepSet", 
        readPreference: "nearest", 
        strategy: "ping"
    }
});

虽然文档指定ping作为默认策略,但似乎Mongoose要求您在使用readPreference时指定一个。

另请注意,secondaryPreferrednearest不同。 secondaryPreferred更喜欢读取辅助成员(顾名思义),无论网络延迟如何,nearest优先读取网络延迟最少的成员。

如果副本集中的配置错误,请确保您的主服务器处于联机且可访问状态 - 默认情况下,如果主服务器处于脱机状态,Mongoose将拒绝使用辅助服务器。

答案 2 :(得分:0)

while(!success){
    try{
        sh = sc.nextShort();
        if(sh < 1 || sh > 100){
            System.out.print("You must enter a value between 1-100");
            throw Exception;
        } //close try
        System.out.println("Great job!");
        success = true; //escape the while
    }catch{Exception ex){
        System.out.println("ERROR: " + ex);
        success = false; //causes infinite loop... I understand
    } //close catch
} //close while

这对我有用