Mongodb (mongolab) Node.js finding nothing on a collection - node.js

I'm a big Node.js and Mongo newbie, so please be gentle.
So here's my Node.js app:
var mongo = require('mongodb');
var Server = mongo.Server;
var Db = mongo.Db;
var server = new Server('hostname.mongolab.com', 666, {auto_reconnect : true}, {w:0, native_parser: false});
var db = new Db('dbName', server, {safe:true});
db.open(function(err, client) {
if(err) { return console.dir(err); }
client.authenticate('mongolabUser', 'mongolabUserPassword', function(authErr, success) {
if(authErr) { return console.dir(authErr); }
var stream = client.collection('myCollection').find({}).stream();
stream.on('data', function(item) {console.log("Do something with item"); });
stream.on('end', function() {console.log("Empty!");});
});
db.close();
});
Through prodigious use of debugger statements, I've come to the conclusion that the client.authenticate doesn't seem to be run. It looks like it's about to execute that line, but then just leapfrogs over it and goes straight to db.close().
But that's just the first of my problems. At some point prior, I was able to connect in to the database and authenticate, but my user was no retrieving anything in the find({}) command. I tried all sorts of ways, and streams are my latest attempt before deciding to give up on it for now.
Mongolab seems to be on v2.0.7, my mongo installation is v2.2.1. When I use the command line tool to log in as mongolabUser and execute a command like db.myCollection.find(), I get everything in my collection, so it can't be an issue with permissions.
Any advice/suggestions?

client.authenticate() is asynchronous, so the line that calls it starts the authentication, but doesn't wait for the server to respond before moving on to executing the next line, db.close(). So by the time the server responds the connection has been closed by the client.
Does moving the db.close() inside the event handler for stream.end help?
var mongo = require('mongodb');
var Server = mongo.Server;
var Db = mongo.Db;
var server = new Server('hostname.mongolab.com', 666, {auto_reconnect : true}, {w:0, native_parser: false});
var db = new Db('dbName', server, {safe:true});
db.open(function(err, client) {
if(err) { return console.dir(err); }
client.authenticate('mongolabUser', 'mongolabUserPassword', function(authErr, success) {
if(authErr) { return console.dir(authErr); }
var stream = client.collection('myCollection').find({}).stream();
stream.on('data', function(item) {console.log("Do something with item"); });
stream.on('end', function() {
console.log("Empty!");
db.close();
});
});
});

Related

mongojs implementation new bie

Hi I am new to MongoDB and NodeJs. I am using "mongojs" module to simple read the data from mongodb. The code goes as follows:
var dbname = 'XXXX';
var databaseUrl = 'mongodb://localhost:27017/'+dbname;
var collections = ['candidate','cities','states','countries'];
var mongojs = require('mongojs');
var db = mongojs(databaseUrl, collections);
db.candidate.findOne({fname:'XXX'},function(err,doc){
if(err){
console.log(err);
}
else{
console.log(doc);
}
});
db.close();
When I run them in the terminal I get this error:
{ name: 'MongoError',
message: 'server localhost:27017 sockets closed' }
Seems like the db.close() is getting executed before the data is fetched. How do we handle such kind of issues.
The above code is asynchronous. While the operation for db.candidate.findOne() is being carried out. The node does not wait for the process to complete and it starts executing another line. So simply putting, db.close() in the callback can solve this error.
var dbname = 'XXXX';
var databaseUrl = 'mongodb://localhost:27017/'+dbname;
var collections = ['candidate','cities','states','countries'];
var mongojs = require('mongojs');
var db = mongojs(databaseUrl, collections);
db.candidate.findOne({fname:'XXX'},function(err,doc){
if(err){
console.log(err);
}
else{
console.log(doc);
}
db.close();
});
I am also a novice in node. So yes, #blake could be correct about what he said, we may never want to close the db connection.

Connect synchronously to mongodb

I would like to connect to mongodb first, then run everything else in my application.
To do it I have to write something like:
MongoClient.connect("mongodb://localhost/test", function(err, connection) {
if (err) { console.error(err); }
db = connection;
var app = express();
// Include API V1
require("./apiv1.js")(app, db);
app.listen(3000, function(err) {
if (err) { console.error(err); } else { console.log("Started on *:3000"); }
});
});
This makes my app to be completely indented inside the .connect function... Which looks ugly and takes space while I work on my project.
I think the best solution would be have the MongoDB connection synchronous (even because witout the DB connection my app cannot work so why should I do something while it's connecting?) and then run the rest of my code.
How can I do?
You can't connect to MongoDB synchronously, but you may get rid of this ugly callback from your code.
The best way to do it is to adopt some wrapper around node-mongodb-native driver.
Take a look at the following modules.
mongojs
var mongojs = require('mongojs');
var db = mongojs('localhost/test');
var mycollection = db.collection('mycollection');
mongoskin
var mongo = require('mongoskin');
var db = mongo.db("mongodb://localhost:27017/test", {native_parser:true});
monk
var monk = require('monk');
var db = monk('localhost/test');
var users = db.get('users')
Of course, internally all of them are establishing MongoDB connection asynchronously.
Using the async library, you can aleve some of these issues.
For example in my server startup I do the following :
async.series([
function(callback){
// Initialize the mongodb connection and callback on completion in init.
db.init(function(){
callback();
});
},
function(callback){
// Listen on requests etc.
webServer.init(function(){
callback();
});
},
function(callback){
// Set up anything else that I need
callback();
}
]);
If you are using Node 6 and up versions, you can do something like this:
const MongoClient = require('mongodb').MongoClient;
const url = 'mongodb://localhost:27017/mydb';
let db = null;
getdb();
//your code
async function getdb() {
db = await MongoClient.connect(url);
}
Bring the mongodb library.
Declare the url constant .
Declare the variable db as null.
Call the getdb function.
Create the getdb function which has firt the async word
Assign to the db variable the result of the connection with the key word await.
You can do it with thunky, thunky executes an async function once and caches it, the subsequent calls are returned from the cache.
const MongoClient = require('mongodb').MongoClient;
const thunky = require('thunky');
var connect = thunky(function(cb){
let url = 'mongodb://localhost:27017/test';
MongoClient.connect(url, function(err, client){
console.log('connecting')
cb(err, client);
})
})
connect( (err, client) => {
console.log('connection 1')
})
connect( (err, client) => {
console.log('connection 2')
})
connect( (err, client) => {
console.log('connection 3')
console.log('closing')
client.close();
})
*Note: I am using latest 3.x mongodb driver

Attempting to return collection with node.js hangs system

I have a node.js application that uses a mongodb database that I've created. Within it, I have a simple collection named comments with the contents { "author": "me", "comment": "this is a comment" } when I call db.comments.find({}).
However, when I attempt to access this collection for display within a jade view I have, it times out after an incrediable amount of time. Console.log for the error object shows it's either a MongoError or connection was destroyed by application. The question I have is why this is happening? I have no errant while loops and connection parameteres seem to check out. Here's what I have to connect with, stored in app.js
var app = express();
var mongodb = require('mongodb'),
serverdb = new mongodb.Server('127.0.0.1', 27017, {}),
db = new mongodb.Db('acl', serverdb, {safe:true});
app.use(function(req,res,next){
req.db = db;
next();
});
and the code I have in the middleware file, stored as a js file in /routes
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res) {
var db = req.db;
var collection = db.collection('comments');
collection.find().toArray(function(err, docs) {
console.log("Printing docs from Array");
if (err) {
console.log(err);
} else {
console.log(docs);
}
});
db.close();
});
module.exports = router;
Like #legalize said, its best to get a mongo connection pool going instead of opening and closing the connection on every request. Perhaps something like this SO answer
As far as why you are getting errors, its probably because your db.close() needs to be in the collection.find().toArray() callback because otherwise it'll start closing the connection before the query even happens.
Lastly, you need to render the template somewhere so the response gets sent back to the client.
Putting it all together, you probably want something like this:
router.get('/', function(req, res) {
var db = req.db;
var collection = db.collection('comments');
collection.find().toArray(function(err, docs) {
console.log("Printing docs from Array");
db.close();
if (err) {
console.log(err);
} else {
console.log(docs);
res.render( 'yourJadeTemplate', { docs : docs } );
}
});
});
(but you really don't want to be closing the connection for every request, especially because you aren't opening it for every request)
Oddly enough replacing this code
var mongodb = require('mongodb'),
serverdb = new mongodb.Server('127.0.0.1', 27017, {}),
db = new mongodb.Db('acl', serverdb, {safe:true});
with this
var db = require("mongojs").connect("localhost:27017/acl", ["comments"]);
made all the difference. No more timeouts. A bit of tweeking to get it to return data.

Can't retrieve data from collection

My problem is that I can't retrieve data from my mongodb database... And I don't know why.
I probably do something wrong, here is a little samble which doesn't work.
var Db = require('mongodb').Db,
Server = require('mongodb').Server;
var db = new Db('akemichat', new Server('localhost', 27017), {w:1});
db.open(function (err, p_db) {
db = p_db;
});
db.collection('rooms', function (err, collection) {
if (!err) {
collection.find().toArray(function(err, items) {
items.forEach(function(room) {
console.log('hello'); // Never call...
});
});
} else {
console.log(err);
}
});
Notice that I have data in my database as shows the following
➜ akemichat git:(master) ✗ mongo
MongoDB shell version: 2.4.7
connecting to: test
> use akemichat
switched to db akemichat
> db.rooms.find()
{ "name" : "home", "_id" : ObjectId("527008e850305d1b7d000001") }
Thanks for help !
Notice: the example program never ends, I don't know why... Maybe because the connection is never closed but if I call the db.close() in the toArray callback, It will never be called because the callback never happends.
So many things in node are asynchronous. Your connection is open after you are trying to read from your collection.
You should query the collection after you know for sure you are connect. Down and dirty:
var Db = require('mongodb').Db,
Server = require('mongodb').Server;
var db = new Db('akemichat', new Server('localhost', 27017), {w:1});
db.open(function (err, p_db) {
db = p_db;
db.collection('rooms', function (err, collection) {
if (!err) {
collection.find().toArray(function(err, items) {
items.forEach(function(room) {
console.log('hello'); // Never call...
});
});
} else {
console.log(err);
}
});
});
I ran this locally and received back the "hello" message. Also your script never finishes because the node process will run until it is closed or crashes. This is by design. Which also means that you don't have to keep opening and closing your mongo connections. You can open a connection when your application starts and close it when your application is shut down.

MongoDb driver in nodejs does not call open function callback

In the following code the function passed to the open function never runs, then the istruction console.log('open!') in the following code never runs:
var mongo = require("mongodb");
var Db = mongo.Db;
var connection = mongo.Connection;
var Server = mongo.Server;
var client = new Db('test', new Server("localhost", 27017, {}));
var test = function (err, collection) {
collection.insert({a:2}, function(err, docs) {
collection.count(function(err, count) {
test.assertEquals(1, count);
});
// Locate all the entries using find
collection.find().toArray(function(err, results) {
test.assertEquals(1, results.length);
test.assertTrue(results[0].a === 2);
// Let's close the db
client.close();
});
});
};
client.open(function(err, p_client) {
console.log('open!');
client.collection('test_insert', test);
});
From the log I see that the connection is accepted:
Sun March 11 16:52:01 [initandlisten] accepted connection from 127.0.0.1:61875 # 1
Mongodb great works from interactive shell.
can someone tell me any suggestion?
thank you!
Copy/paste this...it should work without any problems:
var client = new Db('test', new Server("localhost", 27017, {}), {});
client.open(function(err, client) {
console.log('open!');
}
Also, don't forget to authenticate after opening the connection.
var client = new Db('test', new Server("localhost", 27017, {}), {});
client.open(function(err, client) {
console.log('open!');
client.authenticate('admin', 'admin', function(err, result) {
// Authenticated
});
}

Resources