mongoose.createconnection missing database error - node.js

What I am trying to do is getting list of databases on my localhost server I get the ip address which is in this case local host and port of database server and calling the service Below.
The error I am getting in console is missing database name as I have to connect only database server where am I missing some thing?
app.post('/loadDataBase', function(req,res){
app.set('mongoose').connection.close();
var mongoose = require('mongoose')
, Admin = mongoose.mongo.Admin;
// create a connection to the DB
var connectionStr="mongodb://"+req.body.host+":"+req.body.port;
/* var connection = mongoose.createConnection(
'mongodb://127.0.0.1');*/
var connection = mongoose.createConnection(connectionStr) ;
connection.on('open', function() {
// connection established
new Admin(connection.db).listDatabases(function(err, result) {
// database list stored in result.databases
var allDatabases = result.databases;
res.send(allDatabases);
});
});
});'
app.set('mongoose').connection.close(); Will this close my previous mongoose connection?

This works for me with a mongod running locally on port 27017. It successfully prints out a list of databases, along with their sizeOnDisk.
var mongoose = require('mongoose')
, Admin = mongoose.mongo.Admin;
var connection = mongoose.createConnection('mongodb://localhost:27017') ;
connection.on('open', function() {
// connection established
new Admin(connection.db).listDatabases(function(err, result) {
// database list stored in result.databases
var allDatabases = result.databases;
console.log(allDatabases);
});
});
Can you give me your exact error output? This maybe could help me diagnose what the problem is.
As for your question about closing a previous mongoose connection: you can close the connection with mongoose.disconnect(). However, instead of connecting and disconnecting repeatedly, it is better to connect once when your application starts up and then disconnect when your application shuts down.

Related

MongoDb Node.js drivder - Do I have to close and open connection for each operation?

I am reading node.js mongodb driver tutorial
On the sample code below from the tutorial, it closes the client just after it finishes to do whatever it wants to do.
In case of web-server that constantly interacts with mongo, Is it really expected to reconnect to MongoDB and then close the connection with this procedure each time a request is coming? suggestions for better implementations are welcomed :)
const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');
// Connection URL
const url = 'mongodb://localhost:27017,localhost:27018/?replicaSet=foo';
// Database Name
const dbName = 'myproject';
// Create a new MongoClient
const client = new MongoClient(url);
// Use connect method to connect to the Server
client.connect(function(err) {
assert.equal(null, err);
console.log("Connected correctly to server");
const db = client.db(dbName);
client.close();
});
You can use this, it uses a connection pool there is no need to close the connection.
var mongoose = require("mongoose");
var db = 'mongodb://localhost/dataBaseName';
mongoose.connect(db, {
useNewUrlParser: true
});
var db = mongoose.connection;
db.on("error", console.error.bind(console, "connection error:"));
fs.readdirSync(__dirname + "/models").forEach(function (filename) {
if (~filename.indexOf(".js")) require(__dirname + "/models/" + filename);
});
mongoose.set("useFindAndModify", false);
mongoose.set("useCreateIndex", true);
Not necessary to close and open connection multiple times in same runtime. Connect once when app starts.

NodeJS DB2 Connection pooling for Express app

I see very few online posts when it comes to NodeJS with IBM DB2. I am new to NodeJS and having issues to configure connection pooling for my web app. I am successfully running node app with single connection in my local but not sure how to configure connection pooling. Below code is how I have it for single connection.
DBConnection JS:
module.exports = function(dbConnection)
{
var Pool = require('ibm_db').Pool;
var pool = new Pool();
pool.open("MY_CONNECTION_STRING",function(err,connection){
//error handling logic ...
dbConnection(connection);
});
}
App listener js:
var express = require('express');
var app = express();
app.listen(8080,function(){
console.log("server started..);
});
require('./DBConnection')(function(connection){
app.get('/getEmpId',function(req,res){
connection.query("MY_SQL_QUERY",function(error,results,fields){
//some other logic
res.send(results[0]);
//Closing connection
connection.close(function(err2) {
if(err2) console.log(err2);
});
});
});
}
Need your suggestions to setup connection pool where I can use one connection for each request when concurrent users are accessing and close the connection after serving request.
You can take a look at the brief samples provided with the IBM node-ibm_db driver. It has a section on Connection Pooling. The driver reuses the node-odbc pool and you need to invoke the open/close calls on the Pool object. Here is the sample taken from the node-ibm_db site:
var Pool = require("ibm_db").Pool
, pool = new Pool()
, cn = "DATABASE=dbname;HOSTNAME=hostname;PORT=port;PROTOCOL=TCPIP;UID=dbuser;PWD=xxx";
pool.open(cn, function (err, db) {
if (err) {
return console.log(err);
}
//db is now an open database connection and can be used like normal
//if we run some queries with db.query(...) and then call db.close();
//a connection to `cn` will be re-opened silently behind the scense
//and will be ready the next time we do `pool.open(cn)`
});

How mongodb connection works on concurrent requests in NodeJS express server?

I am new to mongoDB and I'm currently working on setting it up with Node express server. I wonder how to manage concurrent requests to the mongodb to read the collection data using the mongoose driver module.
For example:
If 100 users are accessing my server at a time (http://xxxxxx.com/showusers), how will the mongodb connection in the express server work? Will it be a single connection or split into 100 connections, one for each request?
How can I close the connection object in mongodb efficiently after the operation? Or can we leave the connection in the express server as in the below code?
Here follows my code..
Server.js
var express = require('express');
var app = express();
app.set('port', config.port);
app.get('/users',storeusersapi.showUsers);
app.get('/storeUser',storeusersapi._insertUserDetails);
app.get('/findUser/:email',storeusersapi._findUser);
app.listen(app.get('port'),function(){
log.info('Express app started on port ' + app.get('port'));
});
storeusersapi.js
var mongoose = require('mongoose');
var log = require('../config/logger');
// Mongoose connection to MongoDB (ted/ted is readonly)
mongoose.connect('mongodb://localhost/mydb', function (error) {
if (error) {
log.error("MongoDB Connection failure - " +error);
}else{
log.info('MongoDB is connected Successfully!!!');
}
});
// Mongoose Schema definition
var Schema = mongoose.Schema;
var UserSchema = new Schema({
first_name: String,
last_name: String,
email: String
});
// Mongoose Model definition
var User = mongoose.model('users', UserSchema);
exports.showUsers = function(req,res){
User.find({}, function (err, docs) {
res.json(docs);
});
};
exports._insertUserDetails = function(req,res){
var object = new User({first_name:'bob',last_name:'sel',email:'sel#xxxxx.com'});
object.save(function (err) {
if (err) {
log.error('Insertion error - '+ err);
}
else {
log.info("User Stored into database!!!");
}
});
};
exports._findUser = function(req,res){
User.find({ email: req.params.email }, function (err, docs) {
res.json(docs);
});
};
I have answered for both of your question separately.
1. How will the mongodb connection in the express server work?
Once a connection is created to the mongodb database.(using mongoose or any other framework) It will create a pool of connections with that. (Mongoose default pool size is 5, 100 in python) The created connection pool is maintained by the driver therefore those connections can be re-used when connections to the database are required.
The best practice is to create a new connection only once for the whole application. Once connection is created the connection object will be used as a singleton. When you connect to the database using mongoose models, separate connections are allocated from the created connection pool.
If you are going to create a new connection each time then It will cause to a connection churn.
2. How can I close the connection object in mongodb efficiently after the operation ?
I am not sure 100% about this answer. My suggestion is to disconnect the connection when the express application exits.
var db = mongoose.connect('mongodb://localhost:27017/database');
db.disconnect();
According to my knowledge what you have don in the code is correct. You have created a new connection only once. Since the connection pool is created with that you don't need to create more connections.
Please go through this link to get a clear understanding on connection pools and their usage.
https://dzone.com/articles/deep-dive-connection-pooling

node.js mongo db write concern

I am running node.js 10.22, windows 8 and mongodb not sure what version, but I just downloaded it today, when I run my code I am getting a message, please ensure you set the default write concern, I am trying to follow a YouTube video, and there is mention of this, and I am finding little about it on the internet, from what i found, when I set the db i should set j:true, or safe : true/false, but neither not working for me. I do get the console log that I'm connected and the host and port, but then I get the write concern message and can't type or do anything.
var mongo = require('mongodb');
var host = "127.0.0.1";
var port = mongo.Connection.DEFAULT_PORT;
var db = new mongo.Db("nodeintro", new mongo.Server(host,port,{Fsync: true}));
db.open(function(error){
console.log("we are connected"+host + port);
})
Tried this all type of ways as well, still no luck, best i did was get back to the db write concern message, but was not able to even connect this time. What I'm really looking for is to be able to insert anything in mongo db, and i can figure out the rest.
var Db = require('mongodb').Db;
var Connection = require('mongodb').Connection;
var Server = require('mongodb').Server;
var BSON = require('mongodb').BSON;
var ObjectID = require('mongodb').ObjectID;
var host = "127.0.0.1";
var port = mongo.DEFAULT_PORT;
ArticleProvider = function(host, port) {
this.db= new Db('node-mongo-blog', new Server(host, port, {auto_reconnect: true}, {}));
this.db.open(function(error){
if(error){
console.log(error)
}
else{
console.log(port,host)
}
});
};
ArticleProvider(host,port)
When using mongodb-native directly, you should now use MongoClient.connect to open a database connection pool. It will set a default write concern for you.
var mongodb = require('mongodb');
mongodb.MongoClient.connect('mongodb://localhost/nodeintro', function(err, db) {
// db is your open nodeintro database connection pool here
});
MongoClient was a somewhat recent addition so the tutorial you're working from likely pre-dates it.
If you use {w:1} parameter in your insert or update operation, you might give this error. To overcome you can use {journal:true} parameter in your db settings.
For instance;
var Db = require('mongodb').Db,
MongoClient = require('mongodb').MongoClient,
Server = require('mongodb').Server,
ReplSetServers = require('mongodb').ReplSetServers,
ObjectID = require('mongodb').ObjectID,
Binary = require('mongodb').Binary,
GridStore = require('mongodb').GridStore,
Grid = require('mongodb').Grid,
Code = require('mongodb').Code,
BSON = require('mongodb').pure().BSON;
var db = new Db('Your DB Name', new Server('192.168.170.128', 27017), { journal : true });
db.open(function(err, db) {
var collection = db.collection('user');
collection.findOne({'_id':req.session.User._id}, function(err, user){
// some codes what do you want
collection.save( user, {w: 1}, function(err, user_id) {
// just close the db connection
db.close();
});
});
});

can't connect to mongolab with node.js on heroku

I am having trouble making node.js and mongodb with mongolab work on heroku. I have read other issues like How do I setup MongoDB database on Heroku with MongoLab? and How do I manage MongoDB connections in a Node.js web application? but I still can not set up my connection. In the logs it says [Error: failed to connect to ...]
I have takend the db, host and port from the MONGOLAB_URI process env.I have the following code:
var mongoUri = mongodb://heroku_app17328644:{password}#ds037518.mongolab.com //taken from process.env.MONGOLAB_URI
var host = 'mongodb://heroku_appXXXXXX:{password}#ds037518.mongolab.com';
var port = '37518';
var database = 'heroku_appXXXXXX';
Provider.db = new Db(database, new Server(host, port, { safe: true }, { auto_reconnect: true }, {}));
Provider.db.open(function(err, db){
console.log(db); //null
if (err) console.log(err);
else console.log('success');
});
What am I doing wrong ?
The core issue seems to be that you're trying to use a MongoDB URI as a hostname.
Here's how to connect using a URI and MongoClient:
var mongodb = require('mongodb');
var uri = 'mongodb://user:pass#host:port/db';
mongodb.MongoClient.connect(uri, function (err, db) {
/* adventure! */
});
Of course you'll want to substitute the user, pass, host, port, and db in the uri for your actual connect parameters. If you're using the MongoLab add-on for Heroku you can get the URI from the environment like this:
var uri = process.env.MONGOLAB_URI;
When using MongoClient safe mode is the default, so that option can be left out. To specify auto_reconnect simply pass it as a server option.
var mongodb = require('mongodb');
var uri = 'mongodb://user:pass#host:port/db';
mongodb.MongoClient.connect(uri, { server: { auto_reconnect: true } }, function (err, db) {
/* adventure! */
});
Here's is how I do it. This way, my application connects to the "test" database on my development machine and the "mongolab" database when deployed and running on Heroku.
mongoose = require("mongoose");
mongoURI = 'mongodb://localhost/test';
mongoose.connect(process.env.MONGOLAB_URI || mongoURI);
In my own case, I queried the configuration settings heroku config and it turns out that the mongodb is added as MONGODB_URI.
So, I added process.env.MONGODB_URI to the uri such as:
var uri = process.env.MONGODB_URI || process.env.MONGOHQ_URL || process.env.MONGOLAB_URI;

Resources