Search code examples
node.jssessioncoffeescriptmongohqttl

Error setting TTL index on collection : sessions (MongoDB/MongoHQ)


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

Solution

  • 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";