question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Re-use a native MongoDB driver connection (or a promise)

See original GitHub issue

I am unable to create the following: “Re-use a native MongoDB driver connection (or a promise)”

It always returns the error: “Connection strategy not found”, because in normal Express structure all app.use go before mongoclient.connect (which has app.listen in it).

 var db = {};
 app.use(expresssession({
   secret: 'xxx',
   resave: false,
   saveUninitialized: false,
   store: new mongostore({
      db: db,
      collection: 'sessions',
      ttl: 24 * 60 * 60
   })
}));

mongoclient.connect(mongourl, (error, database) => {
  if (error) return console.log(error);`

  db = database;
  
  app.listen(port, function(){
    console.log('Listening on port: ' + port);
  });
});

The only way I know how to make connect-mongo work with MongoDB driver is by using url instead of db:

store: new mongostore({
    url: mongourl,
    collection: 'sessions',
    ttl: 24 * 60 * 60
  })

but this from all my testing ends up making hundreds of db connections as described in open issue: https://github.com/jdesboeufs/connect-mongo/issues/252

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
jakubrpawlowskicommented, Jan 11, 2018

The only advice I can give you is to use mongoose instead of mongodb. It works with connect-mongo without any issues mentioned above. If you’re not sure how to set it up you can watch this video guide. Good luck!

0reactions
stale[bot]commented, Jun 11, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to properly reuse connection to Mongodb across NodeJs ...
Check mongoist driver. It is "built with async/await in mind" and allows lazily export connection like module.exports = mongoist(connectionString); . (Read ...
Read more >
Node.js Mongodb Driver reuse connection and reconnect to ...
Hello, I'm new to using the node.js mongodb driver. In the past I've used mongoose for all mongodb related projects. I took M220JS...
Read more >
MongoClient or how to connect in a new and better way
mongodb :// is a required prefix to identify that this is a string in the standard connection format. username:password@ is optional. If given,...
Read more >
How do I manage MongoDB connections in a Node js web ...
I'm creating a website with MongoDB and the node-mongodb-native driver. I'd want to know how to manage connections and have a few questions:...
Read more >
connect-mongo - npm
Connection to MongoDB · Create a new connection from a MongoDB connection string · Re-use an existing native MongoDB driver client promise.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found