maysara
maysara

Reputation: 6419

switching between 2 MongoDB on the same server

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

Answers (1)

Nick
Nick

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

Related Questions