Mongoose models from two connections - node.js

I have the following setup:
/models/index.js
var glob = require('glob');
var mongoose = require('mongoose');
var Promise = require('bluebird');
mongoose.Promise = Promise;
mongoose.connect('mongodb://localhost/firstDB', {
useMongoClient: true
});
var models = glob.sync(__dirname + '/*.js', {ignore: '**/index.js'});
models.forEach(function (model) {
require(model);
});
/models/movie.js
var mongoose = require('mongoose');
var movieSchema = new mongoose.Schema({
title: String
}, {timestamps: true});
mongoose.model('movie', movieSchema);
/app.js
require('./models');
...
var Movie = mongoose.model('movie');
...
I would like to access secondDB models from the same app.js file, how can I do that?
Update: The end setup should look something like that:
(not sure how to pass the connection to the model)
/models/index.js
var glob = require('glob');
var mongoose = require('mongoose');
var Promise = require('bluebird');
mongoose.Promise = Promise;
var firstConnection = mongoose.connect('mongodb://localhost/firstDB', {
useMongoClient: true
});
var secondConnection = mongoose.connect('mongodb://localhost/secondDB', {
useMongoClient: true
});
var firstModels = glob.sync(__dirname + '/*.js', {ignore: '**/index.js'});
firstModels.forEach(function (model) {
require(model);
});
var secondModels = glob.sync(__dirname + '/second/*.js');
secondModels.forEach(function (model) {
require(model);
});
/models/movie.js
var mongoose = require('mongoose');
var movieSchema = new mongoose.Schema({
title: String
}, {timestamps: true});
mongoose.model('movie', movieSchema);
/models/second/car.js
var mongoose = require('mongoose');
var carSchema = new mongoose.Schema({
color: String
}, {timestamps: true});
mongoose.model('car', carSchema);
/app.js
require('./models');
...
var Movie = mongoose.model('movie');
var Car = mongoose.model('car');
...

Try this, Thanks!
var mongoose = require('mongoose')
var connection1 = mongoose.createConnection('connectionstring1');
var connection2 = mongoose.createConnection('connectionstring2');
Check this URL - http://mongoosejs.com/docs/connections.html
and Go to Multiple Connection Section.
Edit :
connectionOne.js
var mongoose = require('mongoose'),
mongoURI = 'mongodb://user:password#localhost:27017/dbOne';
module.exports = connectionOne = mongoose.createConnection(mongoURI);
connectionOne.on('connected', function() {
console.log('Mongoose connected to connectionOne');
});
require('./events')
connectionTwo.js
var mongoose = require('mongoose'),
mongoURI = 'mongodb://user:password#localhost:27017/dbTwo';
module.exports = connectionTwo = mongoose.createConnection(mongoURI);
connectionTwo.on('connected', function() {
console.log('Mongoose connected to connectionTwo');
});
require('./events')
events.js
var mongoose = require('mongoose'),
connectionOne = require('./connectionOne');
var Event = new mongoose.Schema({
name: { type: String, required: true },
date: { type: Date, required: false }
});
var connectionTwo = require('./connectionTwo');
var newEvent = new mongoose.Schema({
name: { type: String, required: true },
organizer: { type: String, required: true }
});
module.exports = {
connectionOne.model('Event1', Event);
connectionTwo.model('Event2', newEvent);
}
For more explanation - handling-multiple-databases-and-connections-with-mongoose
Note - I didn't test it.

Related

node.js mongoose find() not value

route/signup.js
var express = require('express');
var router = express.Router();
const User = require('../model/user');
var mongoose = require('mongoose');
function userFind(value){
return User.find({user_id:value}).exec();
};
router.get('/',function (req,res) {
res.render('login/signup');
});
router.post('/',async function (req,res,next){
try{
let userid =req.body.id;
console.log(userid); //abcdefg
const user = await userFind(userid);
console.log(user); // []
}catch(err){
next(err);
}
})
module.exports = router;
model/user.js
const mongoose = require('mongoose')
var Schema = mongoose.Schema;
const userSchema = new Schema(
{ user_id : {type:String,required:true, unique:true},
user_password : {type:String, required:true, select:false},
user_name: {type:String, required:true},
user_email : {type:String,required:true,unique : true},
user_birth : {type:String,require:true},
},{versionKey:false},
{collection: 'user'}
);
module.exports = mongoose.model('user',userSchema);
data inside mongodb.
{
"_id": {
"$oid": "60413c7c48e5e61187cc4eeb"
},
"user_id": "abcdefg",
"user_password": "test111",
"user_name": "세글자",
"user_email": "test#naver.com",
"user_birth": "884455"
}
app.js
var express = require('express');
var mongoose = require('mongoose');
const bodyParser = require('body-parser');
var db = mongoose.connection;
db.on('error', console.error);
db.once('open', function(){
// CONNECTED TO MONGODB SERVER
console.log("Connected to mongod server");
});
mongoose.connect("mongodb+srv://testing1:7894#cluster0.9hxjc.mongodb.net/data?
retryWrites=true&w=majority", { useNewUrlParser: true, useUnifiedTopology:
true });
var app = express();
var test = require('./route/index');
var test2 = require('./route/signup');
app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json());
app.set('views',__dirname+'/views'); // ==
app.set('views',path.join(__dirname, 'views'));
app.set('view engine','ejs');
app.engine('html',require('ejs').renderFile);
app.use('/',test);
app.use('/signup.ejs',test2);
app.listen(3000,function(){
console.log('hello world');
});
Hello, I am studying using mogoose and node.js. I ran mongoose find() but the result was only []. I don't know why this is so, I would appreciate it if you let me know. And if it is findOne(), how should I write it? I tried writing it once, but it turned out to be null.
Through chatting in comments we found the issue. The model is called user:
module.exports = mongoose.model('user',userSchema);
Which mongoose will connect to a collection named users (plural) in MongoDB. That collection was indeed empty. There was only content in a collection named user.

Mongoose create multiple Connections

I'm following Mongoose Connection Docs to create multiple Databases Connections on express app. When i import my schema scoped to a connection i can't use models functions such like .find || .findOne etc.. and i get the next error
UserSchema.find is not a function
Is there something that i miss to add or is something i'm making wrong ??
Here is Schema code:
models/user.js
const { Schema } = require('mongoose');
const UserSchema = new Schema({
name: { type: String},
lastname: { type: String}
});
module.exports = UserSchema;
And then i create the connection with the model scooped to it.
databases/connection_1.js
const mongoose = require('mongoose');
const { Keys } = require('../keys');
const conn = mongoose.createConnection(
`mongodb://${Keys.DatabaseUser}:${Keys.DatabasePassword}#${Keys.DatabaseHost}/${Keys.DatabaseShema}`
, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: true
});
conn.model('User', require('../models/user'), 'Users')
module.exports = conn;
But when i try to use my Schema to fetch the data of the Database i get next error:
UserSchema.find is not a function
routes/index.js
const app = require('express').Router();
const UserSchema = require('../models/user');
app.get('/index', async (req,res) => {
console.log(await UserSchema.find({name: 'Jhon Doe'}))
})
module.exports = app;

Schema has not been registered error on population

When I'm trying to populate , this error is coming:
" MissingSchemaError: Schema hasn't been registered for model "
My models:
/models/course.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var User = require('./user')
var courseSchema = Schema({
courseName:String,
price:Number,
type:String,
_offeredBy:{type:Schema.Types.ObjectId,ref:User}
});
module.exports = mongoose.model("Course",courseSchem
models/user.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var bcrypt = require('bcrypt-nodejs');
var findOrCreate = require('mongoose-findorcreate')
var Course = require('./course')
var userSchema = Schema({
email:{type:String},
password:{type:String},
courseOffered:[{type:Schema.Types.ObjectId,ref:Course}]
});
module.exports = mongoose.model('User',userSchema);
routes/index.js
var Course = require('../models/course');
var User = require('../models/user');
var express = require('express');
var router = express.Router();
router.get('/user/profile',isLoggedIn,function(req,res,next){
res.render('user/profile',{csrfToken:req.csrfToken(),message:""})
});
router.post('/user/profile',isLoggedIn,function(req,res,next){
var courseInfo = req.body;
var newCourse = new Course();
newCourse.courseName = courseInfo.coursename;
newCourse.price = courseInfo.price;
newCourse.type = courseInfo.type;
newCourse._offeredBy = req.user;
newCourse.save(function(err,result){
if(err){
res.redirect('/user/profile');
}
});
Course
.findOne({courseName:courseInfo.coursename})
.populate('_offeredBy')
.exec(function(err,course){
if(err){
res.redirect('/user/profile');
}
});
});
Course is getting saved in the database, but the error is coming due to pouplating. I'm not writing the app.js, mongodb connections are made in app.js file.
Your models are trying to load another model which itself then tries to load the other model.
In your schemas you should set the refs as strings like this:
var courseSchema = Schema({
courseName: String,
price: Number,
type: String,
_offeredBy: { type:Schema.Types.ObjectId, ref: 'User' }
});
var userSchema = Schema({
email: String,
password: String,
courseOffered: [{ type: Schema.Types.ObjectId, ref: 'Course' }]
});
You can then remove the lines to require the other model within each model file. They are both loaded in 'routes/index.js' before being used.
Note: newCouse.save() is asynchronous so you should do you Course.findOne().populate() inside the 'save' callback.

Mongoose multiple connections

Currently I have this code for my connection mongoose.js:
var mongoose = require('mongoose');
var uriUtil = require('mongodb-uri');
var mongodbUri = 'mongodb://localhost/db_name';
var mongooseUri = uriUtil.formatMongoose(mongodbUri);
mongoose.connect(mongooseUri);
module.exports = mongoose;
File that requires the connection is test.js:
var mongoose = require('../model/mongoose');
var schema = mongoose.Schema({...});
How can I update mongoose.js to use multiple connections with mongoose.createConnection(...) function?
I start with changes only for one connection when I do changes like that:
var mongoose = require('mongoose');
mongoose.createConnection('mongodb://localhost/db_name');
mongoose.open('localhost');
module.exports = mongoose;
I get "undefined is not a function".
If I use this code:
var mongoose = require('mongoose');
db = mongoose.createConnection('mongodb://localhost/db_name');
db.open('localhost');
module.exports = mongoose;
I get "Error: Trying to open unclosed connection"
Any advice?
Mongoose handling connections via connections pool
http://mongoosejs.com/docs/connections.html
You can use server: {poolSize: 5} option for increase/decrease pool (number of parallel connections)
If you need connections to different databases look here
Mongoose and multiple database in single node.js project
Example of multiple connections:
var mongoose = require('mongoose')
var conn = mongoose.createConnection('mongodb://localhost/db1');
var conn2 = mongoose.createConnection('mongodb://localhost/db2');
var Schema = new mongoose.Schema({})
var model1 = conn.model('User', Schema);
var model2 = conn2.model('Item', Schema);
model1.find({}, function() {
console.log("this will print out last");
});
model2.find({}, function() {
console.log("this will print out first");
});
OK. With your example I found a solution that fit my needs.
mongoose.js
var mongoose = require('mongoose');
mongoose.main_conn = mongoose.createConnection('mongodb://localhost/main');
mongoose.admin_conn = mongoose.createConnection('mongodb://localhost/admin');
module.exports = mongoose;
content.js
var mongoose = require('../model/mongoose');
var schema = mongoose.Schema({...});
/// functions here
schema.statics.func_a(){...};
schema.statics.func_b(){...};
// And finaly updated only one line
//exports.Content = mongoose.model('Content', schema);
exports.Content = mongoose.main_conn.model('Content', schema);
The only thing, is it OK to add connection objects to mongoose object or may be there is more elegant solution.
config.js
module.exports = {
default: 'main',
main: 'mongodb://localhost/main',
admin: 'mongodb://localhost/admin',
};
connection.js
const mongoose = require('mongoose');
const config = require('./config');
mongoose.Promise = global.Promise;
function createConnection(name) {
return mongoose.createConnection(config[name]);
}
module.exports = createConnection(config[config.default]);
module.exports.on = createConnection;
model.js (custom class)
const connection = require('./connection');
class Model {
constructor(name, data) {
this.data = data;
return this.connection().model(name, data.schema);
}
connection() {
if (this.data.connection) {
return connection.on(this.data.connection);
}
return connection;
}
}
module.exports = Model;
user.js
const Schema = require('mongoose').Schema;
const conn = require('./connection');
const Model = require('./model');
const userSchema = new Schema({
name: String,
email: String,
password: String
});
// USING MONGOOSE MODEL
// default connection
const UserM1 = conn.model('User', userSchema);
// admin connection
const UserM2 = conn.on('admin').model('User', userSchema);
// USING CUSTOM MODEL
// default connection
const UserC1 = new Model('User', {
schema: userSchema
});
// admin connection
const UserC2 = new Model('User', {
schema: userSchema,
connection: 'admin'
});

Creating a Foreign Key relationship in Mongoose

I'm beginning with Mongoose and I want to know how to do this type of configuration:
A recipe has different ingredients.
I have my two models:
Ingredient and Recipe:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var IngredientSchema = new Schema({
name: String
});
module.exports = mongoose.model('Ingredient', IngredientSchema);
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var RecipeSchema = new Schema({
name: String
});
module.exports = mongoose.model('Recipe', RecipeSchema);
Check Updated code below, in particular this part:
{type: Schema.Types.ObjectId, ref: 'Ingredient'}
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var IngredientSchema = new Schema({
name: String
});
module.exports = mongoose.model('Ingredient', IngredientSchema);
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var RecipeSchema = new Schema({
name: String,
ingredients:[
{type: Schema.Types.ObjectId, ref: 'Ingredient'}
]
});
module.exports = mongoose.model('Recipe', RecipeSchema);
To Save:
var r = new Recipe();
r.name = 'Blah';
r.ingredients.push('mongo id of ingredient');
r.save();

Resources