How does the session in the database in ExpressJS automatically clean up?

  node.js, question

Can it be cleaned automatically? The approximate code is as follows:

var mongoStore = require("connect-mongo")(express);
 ...
 app.use(express.session({
 secret  : "foo",
 store   : new mongoStore({
 db  : "bar"
 })
 }));

At ordinary times when using directly is:

exports.blah = function(req, resp) {
 req.session.user = blahblah;
 };

With the passage of time, I found more and more session data in my mongodb’s corresponding collection, which seems to be caused by not clearing?

Therefore, I would like to ask, how can I recycle the session data in the database? THX。

According toDocuments for connect-mongo

By connect/express’s default, session cookies are set to expire when the user closes their browser (maxAge: null). In accordance with standard industry practices, connect-mongo will set these sessions to expire two weeks from their last ‘set’. You can override this behavior by manually setting the maxAge for y ou r cookies — just keep in mind that any value less than 60 seconds is pointless, as mongod will only delete expired documents in a TTL collection every minute.

In other words, by default, Express’s session cookie expires after closing the browser, while mongo deletes the relevant records in two weeks.

If you want the session and the records in the database to fail at the same time, you can do this:

app.use(express.session({
 secret: ......,
 cookie: { maxAge: 1 * 60 * 60 * 1000 }, // 1 hour
 store:   ......
 }))