Question

I'm able to connect to my primary DB no problem, but when I try to connect to my replica set, I get the TTL error. I've done my best to include all relevant code examples, but please ask if you need to see something that's not included. This is driving me bananas. The DB is at mongoHQ.

So, the issue:

  • I can connect to my primary set (workingDB)
  • I cannot connect to my replica set (failingDB)
  • I cannot connect when trying to connect to both(mongoHQ).

Code example

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
Was it helpful?

Solution 2

I was able to resolve the issue by simply removing the reference to the replica set in the URI

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

OTHER TIPS

Perhaps a bit late, but I was today getting similar errors with MongoHQ and Mongoose. I solved it by removing the option mongos: true (at least for the moment, fingers crossed). I think that option is not really needed for replica sets (only when using mongos servers):

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

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

Also, it's better to wait for the connection being established before trying to set up MongoStore, for example:

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);
    });
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top