在集合上设置TTL索引时出错:sessions(MongoDB / MongoHQ)

时间:2014-04-30 14:25:09

标签: node.js session coffeescript mongohq ttl

我能够连接到我的主数据库没问题,但是当我尝试连接到我的副本集时,我收到了TTL错误。我已尽力包含所有相关代码示例,但请询问您是否需要查看未包含的内容。这让我开始香蕉。数据库位于mongoHQ。

所以,问题是:

  • 我可以连接到我的主要设备(workingDB)
  • 我无法连接到我的副本集(failingDB)
  • 尝试连接两者时都无法连接(mongoHQ)。

代码示例

mongoHQ = "mongodb://<user>:<password>@candidate
.14.mongolayer.com:10120/dbName,mongodb://<user>:<password>@candidate
.15.mongolayer.com:10120"

failingDB = "mongodb://<user>:<password>@candidate
.14.mongolayer.com:10120/dbName"

workingDB = "mongodb://<user>:<password>@candidate
.15.mongolayer.com:10120/dbName"


# DB Options
opts =
  mongos: true
  server:
    auto_reconnect: true

# Connect to DB
mongoose.connect mongoHQ, opts


# express/mongo session storage
app.use express.session(
  secret: "Secrets are for children"
  cookie:
    maxAge: process.env.SESSION_TTL * 3600000
    httpOnly: false
  store: new mongoStore(
    url: mongoHQ
    collection: "sessions"
  , ->
    console.log "We're connected to the session store"
    return
  )
)

# Error: Error setting TTL index on collection : sessions

# * Connecting to "workingDB" works as expected.
# * Connecting to "failingDB" throws the same TTL Error
# * candidate.14 is the primary set, candidate.15 is the replica set

2 个答案:

答案 0 :(得分:1)

也许有点晚了,但我今天在MongoHQ和Mongoose上遇到了类似的错误。我通过删除选项mongos: true解决了这个问题(至少目前,手指交叉)。我认为副本集并不真正需要该选项(仅在使用mongos服务器时):

http://mongoosejs.com/docs/connections.html#replicaset_connections

http://support.mongohq.com/languages/mongoose.html

此外,最好在尝试设置MongoStore之前等待建立连接,例如:

mongoose.connect(mongoHQ);
var db = mongoose.connection;

db.on('error', function () {
  // Panic
});

db.on('connected', function() {
    var app = express();
    app.use(express.session(sessionOptions));
    // etc...
    app.listen(config.port, function() {
        console.log('Listening on port ', config.port);
    });
});

答案 1 :(得分:-1)

我只需删除对URI

中副本集的引用即可解决此问题
mongoHQ = "mongodb://<user>:<password>@candidate.15.mongolayer.com:10120/dbName";