Mongoose and multiple database in single node.js project
One thing you can do is, you might have subfolders for each projects. So, install mongoose in that subfolders and require() mongoose from own folders in each sub applications. Not from the project root or from global. So one sub project, one mongoose installation and one mongoose instance.
-app_root/
--foo_app/
---db_access.js
---foo_db_connect.js
---node_modules/
----mongoose/
--bar_app/
---db_access.js
---bar_db_connect.js
---node_modules/
----mongoose/
In foo_db_connect.js
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/foo_db');
module.exports = exports = mongoose;
In bar_db_connect.js
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/bar_db');
module.exports = exports = mongoose;
In db_access.js files
var mongoose = require("./foo_db_connect.js"); // bar_db_connect.js for bar app
Now, you can access multiple databases with mongoose.
Connect multiple mongo db database in node js using mongoose
i believe you are connecting to mongoDB from main entrypoint as index.js or server.js , where you are initiating router. like this
`
const mongoose = require('mongoose')
// mongoose
mongoose.connect("mongoDB url");
const connection = mongoose.connection;
connection.on('open',()=>{
console.log(" database connected")
})
connection.on('error',()=>{
console.log("error in connecting to database")
})
app.use(morgan('dev'));
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
//middlewares`
in same way you can also connect to different databases directly schemas. like in my use case , i wanted to store users in defferent database and posts in another DB .
the in my app.js , i will connect to main DB as normal connection (above) and for user schema , i will connect to my user DB . like this
const mongoose = require('mongoose');
const connection = mongoose.createConnection("mongo url ");
const userSchema = mongoose.Schema({
name: String,
date_of_birth: Date
})
module.exports = mongoose.model('User', userSchema);
you can also use mongoose.connect()
instead of mongoose.createConnection()
hope this helped you.
Related Topics
Change Url Parameters and Specify Defaults Using JavaScript
Node.Js Plans to Support Import/Export Es6 (Ecmascript 2015) Modules
Using Jquery's Ajax Method to Retrieve Images as a Blob
JavaScript Regex: How to Put a Variable Inside a Regular Expression
Document.Createelement("Script") Synchronously
How to Compute the Sum and Average of Elements in an Array
When Does Reflow Happen in a Dom Environment
Logical Operators in JavaScript - How to Use Them
How to Sort an Array on Multiple Columns
How to Destructure Onto an Existing Object? (JavaScript Es6)
How Is Almost Everything in JavaScript an Object
Empty Arrays Seem to Equal True and False at the Same Time
How to Getelementbyclass Instead of Getelementbyid with JavaScript
Seedable JavaScript Random Number Generator
How to Use Jquery for Xml Parsing with Namespaces
How to Get Element by Class Name