Reputation: 306
I have an express app that connects to MongoDB via mongoose, and I also have an init script that will occasionally connect to MongoDB.
Is there any way to detect if there is an existing connection to the DB, so I will not need to connect again in the script anytime I want to run it, also is there any consequences for connecting to the DB multiple times via mongoose.
Upvotes: 4
Views: 5280
Reputation: 15725
you can check this using mongoose.connection.readyState
,
ex.
var mongoose = require('mongoose');
console.log(mongoose.connection.readyState);
The state would return 1 if already connected.
Upvotes: 6
Reputation: 5253
You can check the readyState
var mongoose = require('mongoose');
console.log(mongoose.connection.readyState);
Upvotes: 3