Node.js connects to MongoDB 5 times - node.js

I've been looking for a way to do various operations on a mongo database, depending on which route a user connects to on my website. So doing a html-post to www.mysite.com/data would add info to a mongo DB, and doing a html-get at the same url would get data from the same database. I managed to solve that, but everytime I turn on my server with the website I get 5 connections registred at the mongo database. Why is this, and is it bad?
My code:
I'm runing this code in mongo.js:
var mongodb = require('mongodb');
module.exports.init = function (callback) {
var server = new mongodb.Server("127.0.0.1", 27017, {});
new mongodb.Db('test', server, {w: 1}).open(function (error, client) {
//export the client and maybe some collections as a shortcut
module.exports.client = client;
module.exports.myCollection = new mongodb.Collection(client, 'myCollection');
callback(error);
});
};
I initialize everything running (app.js):
/express set-up/
var mongo = require('./mongo.js');
/.../
mongo.init(function (error) {
if (error)
throw error;
app.listen(3000, function(){
console.log("Express server listening on port %d in %s mode",3000,
app.settings.env);
});
});
And, for example, the post looks like (still app.js):
app.post('/App', function(req, res) {
users = users.concat(req.body);
res.redirect('/App');
//Add user to MongoDB
mongo.myCollection.insert({name: req.body.name, car: req.body.car, website: req.body.website, phone: req.body.phone}, {safe:true}, function(err, objects) {
if (err)
console.warn(err.message);
});
Pretty sure my redirect isn't working as I want it too here, but that's another issue.
Anny suggestions on why I get five connects every time I start the server?

The five connections are because that is the default poolSize (5). You can adjust it, as outlined in the server options docs - it also mentions the default.

Related

App not localhost not loading due to Mongodb connection

I have node app pointing at a mongodb using the connection string:
var url = "mongodb://localhost:27017/";
This variable is used throughout my code:
//When a connection to server is made from client
io.on('connection', (socket) => {
//When host connects for the first time
socket.on('host-join', (data) =>{
//Check to see if id passed in url corresponds to id of game in database
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("agile_poker");
var query = { id: parseInt(data.id)};
dbo.collection('sessions').find(query).toArray(function(err, result){
if(err) throw err;
//A game was found with the id passed in url
if(result[0] !== undefined){
var gamePin = Math.floor(Math.random()*90000) + 10000; //new pin for game
games.addGame(gamePin, socket.id, false, {playersAnswered: 0, questionLive: false, gameid: data.id, question: 1}); //Creates a game with pin and host id
var game = games.getGame(socket.id); //Gets the game data
socket.join(game.pin);//The host is joining a room based on the pin
console.log('Game Created with pin:', game.pin);
//Sending game pin to host so they can display it for players to join
socket.emit('showGamePin', {
pin: game.pin
});
}else{
socket.emit('noGameFound');
}
db.close();
});
});
});
Its been a year since I last ran this locally, but now when I start the app on port 3000, and I type in localhost:3000 or 192.168.0.1:3000 the app doesn't load and instead shows connection refused.
I saw that making HTTP calls to mongodb was deprecated (when I try to hit the mongodb port directly through a browser), but would that cause me the issue I'm experiencing with running my app locally?

Unable to fetch data from mongodb- atlas?

I am trying to fetch data from MongoDB-atlas using note.js which I have manually fed to the server.
The status of the collection on MongoDB-atlas sever is :
dbname: viit
collection name : viit_atts
also, I have whitelisted my id and I am able to connect to the server.
I started off by connecting to the server by using the command :
mongoose.connect("mongodb+srv://wimpy_cool:myPassWordGoesHere#cluster0-phqid.mongodb.net/test?retryWrites=true&w=majority",{},function(err,res)
{
if(err)
{
console.log("mongo lab server not connected");
console.log(err);
}
else
{
// console.log(res);
console.log("Connectd to mongolab db");
}
});
Now using mongoose I have declared schema and made a model and then use the find function to fetch the data.
var bodyParser = require('body-parser');
var mongoose =require("mongoose");
var express= require("express");
var app = express();
app.use(bodyParser.urlencoded({extended:true}));
mongoose.connect("mongodb+srv://wimpy_cool:myPassWordGoesHere#cluster0-phqid.mongodb.net/test?retryWrites=true&w=majority",{},function(err,res)
{
if(err)
{
console.log("mongo lab server not connected");
console.log(err);
}
else
{
// console.log(res);
console.log("Connectd to mongolab db");
}
});
var viit_att_schema = new mongoose.Schema({
name : String,
no_of_present: Number,
no_of_absent: Number
});
var att_history_schema = new mongoose.Schema({
time : String ,
att_stats : String,
});
var viit_att= mongoose.model("viit_att",viit_att_schema);
var att_history = mongoose.model("att_history",att_history_schema);
var list_of_all_members;
var list_of_all_members_sorted;
viit_att.find({},function(err,viit_atts)
{
if (err) {
console.log("OH NO ERROR");
}
else
{
console.log("fetching data now");
console.log(viit_atts);
list_of_all_members=viit_atts;
// console.log(list_of_all_members[0]);
// console.log(list_of_all_members);
list_of_all_members_sorted = list_of_all_members.sort(function(a,b) {
return b.no_of_present - a.no_of_present ;
});
console.log(list_of_all_members_sorted);
}
});
app.listen( process.env.PORT || 3000 , function(){
console.log("SERVER 3000 HAS STARTED");
});
But the fetching doesn't seem to take place, the console says this :
D:\MY PROJECTS\WEB\club_attendence_website>node backend.js
(node:13076) DeprecationWarning: current URL string parser is deprecated,
and will be removed in a future version. To us
e the new parser, pass option { useNewUrlParser: true } to
MongoClient.connect.
SERVER 3000 HAS STARTED
Connectd to mongolab db
fetching data now
[]
[]
The problem was that the default connection string for MongoDB atlas has a default database name as test, and will search for the same in the cluster, hence even though it will connect, nothing will be fetched.
In order to learn how to explicitly mention the dbName refer to this link: Fail to connect Mongoose to Atlas
Hi I had the same issue!
Issue:
I created a mongoDb Atlas collection name is "main"; however when I connect my nodeJs using mongoose, the collection name from the callback became "mains".
Solution:
I changed my model name to "mains" and it worked.
Try this:
‍var viit_att= mongoose.model("viit_atts",viit_att_schema);‍
Try doing it like this:
viit_att.find({}).then(content => {
console.log("data was fetched");
console.log(content);
list_of_all_members=content;
// console.log(list_of_all_members[0]);
// console.log(list_of_all_members);
list_of_all_members_sorted = list_of_all_members.sort(function(a,b) {
return b.no_of_present - a.no_of_present ;
});
console.log(list_of_all_members_sorted);
})
.catch(err => { console.log("OH NO AN ERROR") })
I got into this same problem, what worked for me, was to specify a collection name when defining your mongoose models:
module.exports = mongoose.model('App', schema, COLLECTION_NAME)
Where COLLECTION_NAME must match the mongodb atlas collection name.
For me the problem was that I was unable to fetch or get data but the connection was established with the server and I found that the few problems that can occur are:-
1: URL encoding
The text you put inside the connection string must be url encoded as said in the mongodb docs.
So, if the parameters you put inside the connection string like your database name or your password, the special characters that are : / ? # [ ] # must be url encoded (also known as percentage encoded), e.g. # becomes %40. You can use a bunch of online url encoders which are available on the internet.
2: Not added IP address from mongodb UI
You have to add the IP address of the computer from which the database will be sent a request. You can either do it in the first step while connecting by clicking 'Connect' on the cluster page or add ip address from here.
I personally kept it to be accessible by everyone, so added the ip address 0.0.0.0/0 (includes your current IP address).
3: Not specifying the correct database name
If you paste the default connection string in your app, it will point to the default database that is test. You will have to either change test to your db name or if there's nothing written, you'll have to write your db name
So, your connection string should look like this:-
mongodb+srv://<Your username>:<Your password>#cluster0.ezmvoas.mongodb.net/<Your db name>?retryWrites=true&w=majority
Troubleshoot
If there is any other problem, what you can do is to give error as an input to the callback function and try logging the error on the console.
const connectToMongo = () => {
mongoose.connect(mongoURI, (error, result) => {
console.log('Connected to MongoDB' + '\nError: ' + error);
});
}
By this way, you can get the fix for exactly the error message without trying different things.
Hope this solves the problem,
Thanks!

How do I get a MongoDB import script to close the db after inserting results?

I am running a quick little nodejs script to find documents in one collection and insert them into another collection but on the same DB. I came up with this guy, but it has no way to close because I think its running open or async?
I have tried placing the db.close() in various places and tried mongoClient.close(). No luck which had me thinking about trying to force a timeout for the async call. Added a connection Time out but it did not have the desired behaviour.
var MongoClient = require('mongodb').MongoClient
, assert = require('assert');
const async = require("async");
// Connection URL
var url = 'mongodb://localhost:27017/sourceDB';
// Use connect method to connect to the Server
MongoClient.connect(url,{connectTimeoutMS: "5"}, (err, db) => {
db.collection('source.collection', function(err, col) {
assert.equal(null, err);
col.find().forEach(function (data) {
console.log(data);
db.collection('destination.collection').insertOne(data, function(err, res) {
assert.equal(null, err);
});
console.log("Moved");
});
});
});
The script does well and picks up the collection and inserts, but the connection remains open.
It is not recommended to explicitly close the connection as shown by this SO thread.
Rather, allow the client library to manage the connection for you.

Disconnect PostgreSQL when client goes to new page

I'm trying to build a real time web page and use postgreSQL as my database. I use node.js and express to build backend stuff. Since this is a real time webpage and needs to update information very frequently, I keep a long connection with postgreSQL, which looks like:
app.get('/:A/:B', function(req,res){
var A = req.params.A;
var B = req.params.B;
var client = new pg.Client(config[A][B]);
client.connect(function(err){
if (err) {
console.log("Error occurred when try to connect the database",err);
}
else {
console.log("Connected to the database");
}
});
Do some queries with current database connection...
}
The problem is, when I change the value of A and B in browser and try to connect to a new database, I didn't disconnect with the old one so the info on my page are still from the old database. I'm new to node and web development. Can anyone let me know how to disconnect with the old database when client try to go to a new url?
I think is not good way to create connection for each request. If size of A-B variants is limited then create of connection pool on start is better.
app.get('/:A/:B', function(req, res, next){ // next to forwarding error
var A = req.params.A;
var B = req.params.B;
var client = new pg.Client(config[A][B]);
client.connect(function(err){
if (err)
return next(err); // go to error-middleware
console.log("Connected to the database");
// Do some queries with current database connection...
// Keep it mind that they're also asynchronous, so better way is use promises or async (https://github.com/caolan/async)
client.end(function (err) {
if (err)
next(err);
});
});
}
// Error middleware
app.use(function(err, req, res, next) {
console.log(req.url, err.message);
})

Improving performance of inserting into Mongo from ActiveMQ

The basic idea of the following code is I read messages off an ActiveMQ Artemis installation and insert them into a MongoDB instance.
It works well for up to a hundred or so messages per second but crashes if I throw a few thousand at it. My first guess would be the constant opening and closing of database connections. Should I also think about using an in-memory store and doing bulk database inserts?
The code is all running in node using the mqtt and mongodb npm packages. The code below, the database and the queue are all running in docker containers if it makes any difference.
var mqtt = require('mqtt'),
client = mqtt.connect('mqtt://mq:1883', {
username: "*************",
password: "*************"
}),
MongoClient = require('mongodb').MongoClient,
ObjectId = require('mongodb').ObjectID,
assert = require('assert'),
url = 'mongodb://db:27017/uo-readings';
client.on('connect', function () {
client.subscribe('readings');
});
client.on('error', function(error){
console.log(error)
});
client.on('message', function (topic, message) {
console.log(message.toString());
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
console.log("Connected correctly to server.");
db.collection('readings').insertOne(JSON.parse(message.toString()), function(err, result) {
assert.equal(err, null);
console.log("Inserted a document into the readings collection.");
});
client.end(function(){
console.log("Closing Connection.");
db.close();
});
});
});
See #Jonathan Muller's comment above

Resources