Monday 12 June 2017

Express session store: MongoError: E11000 duplicate key error collection on _id

I am using sessionstore npm module to store the session data in the database.

var session = require('express-session');
var sessionstore = require('sessionstore');

app.use(session({
  secret:appConfig.JWT_SECRET,
  resave:false,
  saveUninitialized:true,
  store: sessionstore.createSessionStore({
      type: 'mongodb',
      host: 'localhost',
      port: 27017,
      dbName: 'MyDatabase',
      collectionName: 'sessions',
      timeout: 10000
  }),
  cookie: {
         maxAge: 1000 * 60 * 60 // Milliseconds (3600000ms -> 60min)
     }
}));

and i am getting this error :

MongoError: E11000 duplicate key error collection: myDatabase.sessions index: _id_ dup key: { : "Lz4SJIHaBfGFfNg3_ChNTkFnwKBLQDUN" }
    at Function.MongoError.create (/home/user/public_html/projectName/server/node_modules/mongodb-core/lib/error.js:31:11)
    at toError (/home/user/public_html/projectName/server/node_modules/mongodb/lib/utils.js:139:22)
    at /home/user/public_html/projectName/server/node_modules/mongodb/lib/collection.js:1060:67
    at /home/user/public_html/projectName/server/node_modules/mongodb-core/lib/connection/pool.js:461:18
    at _combinedTickCallback (internal/process/next_tick.js:73:7)
    at process._tickDomainCallback (internal/process/next_tick.js:128:9)

How to prevent creating documents with the same id. Also i am not using Mongoose in my project. What can be done as this does not crash the server but close down all the connections with the database and the users are not able to login (or there is no api response).

I also check some similar questions on SO as well as github but they all point towards mongoose or change in field name but there is no such thing in my case.

Please help.



via Aman Gupta

No comments:

Post a Comment