Re-use a native MongoDB driver connection (or a promise)
See original GitHub issueI 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:
- Created 6 years ago
- Comments:8
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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!
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.