How to interact with two mongodb server (mongoose) on one node server - node.js

I am using Mongoose. I have two Mongo DB Server i.e
Primary(27017, 27018, 27020 {replica set enabled on primary}) & Secondary(27021)
Now I want to interact with these two server through API in nodejs. Lets say
router.post('/connectToDB1', function (req, res) {
//Some logic for interacting with DB 1
})
router.post('/connectToDB2', function (req, res) {
//Some logic for interacting with DB 2
})
On my node server what I implemented is like this:
//On my app.js File
var Mongoose = require('mongoose').Mongoose;
Mongoose.Promise = global.Promise;
var Message = require('./models/message');
var mydb1;
var mydb2;
var instance1 = new Mongoose();
var uri1 = 'mongodb://127.0.0.1:27017, 127.0.0.1:27018, 127.0.0.1:27020/myDBName?replicaSet=newRep';
instance1.connect(uri1, { useMongoClient: true }).openUri(uri1, function (errr,db) {
if (errr) {
throw errr;
} else {
console.log("Primary Connection Successfull");
mydb1 = instance1;
}
});
var instance2 = new Mongoose();
var uri2 = 'mongodb://127.0.0.1:27021/secondary';
instance2.connect(uri2, { useMongoClient: true }).openUri(uri2 , function (errr,db) {
if (errr) {
throw errr;
} else {
console.log("Secondary Connection Successfull");
mydb2 = instance2;
}
});
Now I don't Know how to Interact/create A Query...
Any help is really Apreciated.

Mongoose has a function called createConnection() that returns a connection object. You can create multiple connections to different servers/replicaSets and create models specific to each connection. Here is an example:
49790706.js
#!/usr/bin/env node
'use strict';
const mongoose = require('mongoose');
const conn1 = mongoose.createConnection('mongodb://localhost:27017/test1');
const conn2 = mongoose.createConnection('mongodb://localhost:27018/test2');
const Schema = mongoose.Schema;
const userSchema = new Schema({
name: String
});
const adminSchema = new Schema({
name: String
});
const User = conn1.model('User', userSchema);
const Admin = conn2.model('Admin', adminSchema);
const billy = new User({ name: 'Billy' });
const root = new Admin({ name: 'root' });
async function run () {
await conn1.dropDatabase();
await conn2.dropDatabase();
await billy.save();
await root.save();
let b = await User.find({});
let r = await Admin.find({});
console.log('users:', b);
console.log('admins:', r);
return mongoose.disconnect();
}
run();
shell output
stack: ./49790706.js
users: [ { _id: 5acf1b72bb7d5f546c1a526f, name: 'Billy', __v: 0 } ]
admins: [ { _id: 5acf1b72bb7d5f546c1a5270, name: 'root', __v: 0 } ]
stack:
You need to be sure that when you create your models you attach them to the specific relevant connection, ie conn1.model(..). Also, when your app is ready to close the connections rather than call .close() on each one, note that you can call mongoose.disconnect() and it will close all connections in parallel.

Related

Mongoose is not connecting to local database

This is my app.js file. Please help me out to connect it with my local database. Sometimes it gets connected to the database and logs to the console but it doesn't add any collection to the local database.
const mongoose = require('mongoose')
main().catch(err=>console.log(err))
async function main() {
await mongoose.connect("mongodb://localhost:27017/fruitsDB", {
useNewUrlParser: true,
useUnifiedTopology: true
});
//Creating new schema
const fruitSchema = new mongoose.Schema({
name: String,
rating: Number,
review: String
});
const Fruit = mongoose.model("Fruit", fruitSchema);
const fruit = new Fruit ({
name: "Apple",
rating: 7,
review: "Pretty solid"
});
await fruit.save()
}
Insist localhost use 127.0.0.1:27017 This will work for sure.
OR
This happened probably because the MongoDB service isn't started. Follow the below steps to start it:
Go to Control Panel and click on Administrative Tools.
Double-click on Services. A new window opens up.
Search MongoDB.exe. Right-click on it and select Start
The server will start. Now execute npm start again and the code might work this time.
You can use mongo connection like this in typescript for ES6.
Schema like below
import mongoose from "mongoose"
export const RequestLogsSchema = new mongoose.Schema(
{
request_id: String,
...
},
{
collection: "request_logs"
}
)
example connection like below
import mongoose from 'mongoose'
import { RequestLogsSchema } from './mongo-schemas/RequestLogsSchema'
export class MongoClient {
mongodb: any
constructor(private host: string) { }
async MongoConnect() {
return new Promise(async (resolve, _reject): Promise<void> => {
console.log('🟡 MongoDB Connecting !')
this.mongodb = await mongoose.connect(this.host).then(() => {
console.log('🟢 MongoDB Connected !')
resolve(true)
}).catch((err) => console.log(err))
})
}
}
export const schemas = {
RequestLogsModal: mongoose.model("RequestLogs", RequestLogsSchema),
...
}
new MongoClient('mongodb://username:password#localhost:27017/db_name?authSource=db_name').MongoConnect()
To save your data like
import { schemas } from '../connections/mongo'
const saver = (data) => {
const request_logs = new schemas.RequestLogsModal({
request_id: data.request_id,
...
})
await request_logs.save()
}

Mongoose query doesn't work with the filter

I'm testing a nodejs snippet to do iteration with my example mongodb collection users. But the query never worked. The full users collection are printed.
The standalone mongodb is setup in EKS cluster.
Why the query {name: "Baker one"} didn't work?
The code is:
const mongoose = require("mongoose");
const url = "mongodb://xxxxxx:27017/demo";
main().catch(error => console.error(error.stack));
async function main() {
// Connect to DB
const db = await mongoose.connect(url);
console.log("Database connected!");
const { Schema } = mongoose;
// Init Model
const Users = mongoose.model("Users", {}, "users");
const users = await Users.find({name: "Baker one"}).exec();
// Iterate
for await (const doc of users) {
console.log(doc);
console.log("users...");
}
console.log("about to close...");
db.disconnect();
}
The users collection:
The execution result:
$ node modify.js
Database connected!
{ _id: new ObjectId("610f512c52fa99dcd04aa743"), name: 'Baker one' }
users...
{ _id: new ObjectId("61193ed9b8af50d530576af6"), name: 'Bill S' }
users...
about to close...
This line could be the culprit (note that the schema has been defined with no fields like Joe pointed out in the comments).
const Users = mongoose.model("Users", {}, "users");
In this case, I had to add StrictQuery option in the Schema constructor to make it work, like so:
const userSchema = new Schema({}, { collection: "Users", strictQuery : false });

create dynamically mongo collection in mongoose

create dynamically mongo collection in mongoose. I tried below code: suggest me please
function residenceModel(regionName){
const residenceSchema = new Schema({
key:String,
value:String
}, { strict: false });
return mongoose.model(
`residence_meta_${regionName}`,
residenceSchema,
`residence_meta_${regionName}`
);
}
console.log(residenceModel);
exports.ResidenceModel = residenceModel;
You can create dynamic model with this simple steps
create a file dynamicModel.js
const mongoose = require('mongoose')
//import mongoose from 'mongoose'
const Schema = mongoose.Schema
const dynamicModel = (regionName) => {
const residenceSchema = new Schema({
key:String,
value:String
}, { strict: false })
return mongoose.model(
`residence_meta_${regionName}`,
residenceSchema,
`residence_meta_${regionName}`
);
}
module.exports = { dynamicModel }
// export default { dynamicModel }
On file where you want to include or create model let's say app.js
// Please take care of path
const { dynamicModel } = require("./dynamicModel")
//import { dynamicModel } from "./dynamicModel";
.....
.....
// User this method to create model and use It
const countryModel = dynamicModel("country")
countryModel.create({ key:"country", value:"USA" });
Use this method multiple time, Give it a shot and let me know
I fixed this issue. below is the code. you guys can use it.
function createMetaCollection(regionName){
console.log(regionName);
var residenceSchema = new Schema({
region:String,
customer_type:String,
},{strict: false});
residenceSchema.plugin(autoIncrement.plugin,{model:'resident_meta_'+regionName,field:regionName+'id',startAt:1});
exports.Typeof_employment_meta = mongoose.model('resident_meta_'+regionName,residenceSchema,'resident_meta_'+regionName);
}

I was writing code for database through mongoose but I am getting this error on the terminal mongoose.connection.on error

I am getting this error every time I start the app.....error is in the screenshots. Please let me know how to fix this up.Thanks in advance.enter image description here
enter image description here
You got error because Mongoose.connection is not yet exists when you try to access it. You need either use callback as
const Mongoose=require("mongoose").connect(config.dbURL,(error)=>{
if (!error) {
Moongose.connection.on("error",(error)=>{...your code here ..});
}
})
or use Promise
const Mongoose=require("mongoose").connect(config.dbURL)
.then(()=>{
Moongose.connection.on("error",(error)=>{...your code here ..});
});
You should definitely use some variables/identifiers to store the connection as explained in the Mongoose documentation here.
console.log your config and check whether they are being properly fetched.
The community also helps better if you provide more details and use code snippets instead of screenshots.
Next, check if your Database URL starts with mongodb://.
And then, you have a spelling error on line 19 where you define schema charUser but are trying to model chatUser on line 27.
Try putting Mongoose.promise = Promise; just after you import mongoose.
Also, I'd suggest you to use the following style:
1. Create db.js which will export the database connection for you.
const mongoose = require('mongoose');
mongoose.Promise = Promise;
const options = {
poolSize: 25,
socketTimeoutMS: 0,
keepAlive: true,
reconnectTries: 30,
user: 'MONGODB_USER',
pass: 'MONGODB_PASS',
auth: {
authdb: 'MONGODB_AUTH_DB',
},
};
const dbname = 'db_name_goes_here';
const host = 'db_server_URI_goes_here';
const port = port_number_goes_here || 27017;
const uri = `mongodb://${host}:${port}/${dbname}`; // a template string
const db = mongoose.createConnection(uri, options);
module.exports = db;
2. Use the connection in the Model file in this manner:
const { Schema } = require('mongoose');
const db = require('./db.js');
const chatUser = new Schema({
profileID: String,
fullName: String,
profilePic: String
});
const userModel = db.model('chatUser', chatUser);
module.exports = { userModel };
Try this:-
const MongoClient = require('mongodb').MongoClient;
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'test';
// Use connect method to connect to the server
MongoClient.connect(url, function(err, client) {
assert.equal(null, err);
console.log("Connected successfully to server");
const db = client.db(dbName);
});

How to modulize my mongoose setup

This use of db.model('Model_Name').find(...) worked before, but I've since split my database, model and controllers up, so now it seems I need to change things around Please help show where I'm going wrong:
schema.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId;
var UsersSchema = new Schema({
display_name : String,
email : String
});
var ClientErrorsSchema = new Schema({
datetime : Number,
msg : String,
url : String,
line : Number
});
var users = mongoose.model('Users',UsersSchema);
var client_errors = mongoose.model('Client_Errors',ClientErrorsSchema);
module.exports = mongoose.model;
db.js
var mongoose = require('mongoose');
var model = require("./schema.js");
var MONGO = {
username: "admin",
password: "***",
server: 'localhost',
port: '*****',
db: 'db_name',
connectionString: function(){
return 'mongodb://'+this.username+':'+this.password+'#'+this.server+'/'+this.db;
},
options: {
server:{
auto_reconnect: true,
poolSize: 20,
socketOptions:{
keepAlive: 1
}
},
db: {
numberOfRetries: 10,
retryMiliSeconds: 1000
}
}
};
var db = mongoose.createConnection(MONGO.connectionString(), MONGO.options);
db.model = model;
db.on('error', function(err) {
f.consoleLogger("DB connection Error: "+err);
});
db.on('open', function() {
f.consoleLogger("DB connected");
});
db.on('close', function(str) {
f.consoleLogger("DB disconnected: "+str);
});
module.exports = db;
user.js
var db = require("./db.js");
...
db.model('Users').find(...)
So using the util.inspect, I can see the db has all of the models, but the error I'm getting on the find is TypeError: Cannot read property 'User' of undefined'.
UPDATE
Toad22222 provided great advice, and the schema appears to be valid - but the queries are not working - neither db.model.Users.find({...},function(err,data){...}); or db.model.Users.find({...}).exec(function(err,data){...}); fires the callback. I have put the same code in the on('open') event as well, nothing happens. Hopefully someone can explain why.
All advice appreciated!
Try out
module.exports = {
User: users,
ClientErrors: client_errors
}
instead of
module.exports = mongoose.model;
You want to be exporting the schemas you are creating, not the mongoose.model datatype.

Resources