MongoDB Connection Error While Connect to Nodejs - node.js

As i'm new to api creation in nodejs and i tried to connect mongo db throung nodejs code and i have advance version of mongodb(V4) and nodejs(v10) and i could not able to connect please can any one help me.
my mongodb node code:
// Retrieve
var MongoClient = require('mongodb').MongoClient;
// Connect to the db
MongoClient.connect("mongodb://localhost:27017/exampleDb", function(err, db) {
if(!err) {
console.log("We are connected");
}
});
and cmd:
commend for db connection in cmd prompt is mongod -- for connection establishement.

Related

MongodbClient.connect() not working with mongodb locally

I am trying to connect to mongodb server locally at this ip mongodb://127.0.0.1:27017/ yet still no connection to the mongodb server
I have tried using localhost instead of 127.0.0.1 but not change .
const MongoClient = require('mongodb').MongoClient;
const url = 'mongodb://127.0.0.1:27017/';
const dbname = 'conFusion';
console.log("before connect");
MongoClient.connect(url, (err, client) => {
console.log('Connected SuccessFully');
});
I have also run this command Mongod --dbpath=data --bind-ip 127.0.0.1:27017 but no change
Occasionally I would get the timeout error

How to define mongoDB database connection variable globally

I have worked to node js + mysql. but now i am going to node js + mongoDB
I am in need to connect database and make a variable for globally use.
I am very much confused how we can define variable for globally use.
var mongodb = require('mongodb');
var MongoClient = mongodb.MongoClient;
var url = 'mongodb://localhost:27017/mydb';
MongoClient.connect(url, function (err, db) {
if (err) {
console.log('Unable to connect to the mongoDB server. Error:', err);
} else {
//HURRAY!! We are connected. :)
console.log('Connection established to', url);
// do some work here with the database.
db.close();
}
});

Unable to connect to the mongoDB server. Error: [Error: connection closed]

This is my nodejs program it is giving an error:
var mongodb = require('mongodb');
var server = Server = require('mongodb').Server;
var MongoClient = mongodb.MongoClient;
var url = 'mongodb://localhost:27017/prisync_mamy';
MongoClient.connect(url, function (err, db) {
if (err) {
console.log('Unable to connect to the mongoDB server. Error:', err);
} else {
console.log('Connection established to', url);
var collection = db.collection('urlAnalysis_mamy');
var cursor = collection.find({channel_name:'urlAnalysis_mamy'});
cursor.each(function (err, doc) {
if (err) {
console.log(err);
} else {
console.log('Fetched:', doc);
}
});
}
});
Error: [Error: connection closed]
but when i type mongo on terminal it is running fine.
In my case, the problem was file security on the new server and mongdb npm module files, so I had to use "chown" command to own all files and then run "npm install", after that MongoDB client worked properly.
The exception from running your code is
TypeError: db.collection is not a function
and that tells you the collection keyword do not exist in the variable db. Look at the mongodb npm page and you should do this:
var mongodb = require('mongodb');
var server = Server = require('mongodb').Server;
var MongoClient = mongodb.MongoClient;
var url = 'mongodb://localhost:27017/prisync_mamy';
// Use connect method to connect to the server
MongoClient.connect(url, function (err, client) {
console.log("Connected successfully to server");
const db = client.db('prisync_mamy');
...
});

Unable to connect to mongodb via express project

Tracing the documentation here and failing to load bson apparently. After running npm start I receive:
Snippet:
var mongo = require("mongodb").MongoClient;
//connect to db server
mongo.connect("mongodb://localhost:28017/myDb", function(err, db){
if(!err) {
console.log("Connected to Database")
}
else{
console.log("failed to connect");
}
});
I have tried updating/reinstalling the driver modules as well. Totally new to the framework & db and this type of error feels so trivial that it is discouraging that I am unable to figure it out. Help!
The default port for mongodb is 27017 (and then 28017 is for a web status page).
http://docs.mongodb.org/manual/reference/default-mongodb-port/
Try this connect string:
"mongodb://localhost:27017/myDb"

mongoose.createconnection missing database error

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.

Resources