Reputation: 6419
I am building a simple Nodejs CMS, to create/delete records for the production and for the development server, the production and the development servers have different Databases with the same model schemas but with different records, I just want to be able to switch between the DB connections to be able to create/delete the records for both of the databases (production/development) using the CMS server.
I am using Nodejs on the server and mongoose as an ORM.
So how can I manage different databases connections from the same server?
Upvotes: 0
Views: 231
Reputation: 352
You can use mongoose.createConnection
example:
//Main DB Connection
var uriToFirstDB = "http://localhost:27017/firstDB"
mongoose.connect( uriToFirstDB, options )
//Second Connection
var uriToOtherDB = "http://localhost:27017/anotherDB"
var secondaryDBConnection = mongoose.createConnection( uriToOtherDB, options )
var firstCollection = mongoose.model( "firstCollection", firstSchema )
var secondCollection = secondaryDBConnection.model( "secondCollection", secondSchema)
console.log( secondCollection.find({}).count() )
Upvotes: 1