Configure expired session removal (autoRemove)
masterYou can control how expired sessions are cleaned up using the autoRemove option:
native(Default): Uses MongoDB's TTL collection feature. Requires MongoDB 2.2+ and admin permissions.connect-mongocreates the TTL index for you. Avoid this in highly concurrent environments; instead, manage the index manually.interval:connect-mongohandles removal at a defined interval. Useful for environments like Azure Cosmos DB that don't support TTL indexes. UseautoRemoveInterval(in minutes, default 10) to set the frequency.disabled: Disables automatic cleaning. Use this if you manage the TTL index elsewhere or in production environments where you want manual control.
// Native mode (Default)
app.use(session({
store: MongoStore.create({
mongoUrl: 'mongodb://localhost/test-app',
autoRemove: 'native'
})
}));
// Interval mode
app.use(session({
store: MongoStore.create({
mongoUrl: 'mongodb://localhost/test-app',
autoRemove: 'interval',
autoRemoveInterval: 10 // In minutes. Default
})
}));
// Disabled mode
app.use(session({
store: MongoStore.create({
mongoUrl: 'mongodb://localhost/test-app',
autoRemove: 'disabled'
})
}));