Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looking for help with reading from MongoDB in Node.JS

I have a number of records stored in a MongoDB I'm trying to output them to the browser window by way of a Node.JS http server. I think I'm a good portion of the way along but I'm missing a few little things that are keeping it from actually working.

The code below uses node-mongo-native to connect to the database.

If there is anyone around who can help me make those last few connections with working in node I'd really appreciate it. To be fair, I'm sure this is just the start.

var sys  = require("sys");
var test = require("assert");
var http = require('http');

var     Db              = require('../lib/mongodb').Db,
        Connection      = require('../lib/mongodb').Connection,
        Server          = require('../lib/mongodb').Server,
        //BSON          = require('../lib/mongodb').BSONPure;
        BSON            = require('../lib/mongodb').BSONNative;

var     host = process.env['MONGO_NODE_DRIVER_HOST'] != null ? process.env['MONGO_NODE_DRIVER_HOST'] : 'localhost';
var     port = process.env['MONGO_NODE_DRIVER_PORT'] != null ? process.env['MONGO_NODE_DRIVER_PORT'] : Connection.DEFAULT_PORT;

sys.puts("Connecting to " + host + ":" + port);

function PutItem(err, item){
    var result = "";
    if(item != null) {
            for (key in item) {
                    result += key + '=' + item[key];
            }
    }
    // sys.puts(sys.inspect(item))  // debug output
    return result;
}

function ReadTest(){
    var db = new Db('mydb', new Server(host, port, {}), {native_parser:true});
    var result = "";
    db.open(function (err, db) {
            db.collection('test', function(err, collection) {
                    collection.find(function (err, cursor){
                            cursor.each( function (err, item) {
                                    result += PutItem(err, item);
                            });
                    });
            });
    });
    return result;
}

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end("foo"+ReadTest());
}).listen(8124);
console.log('Server running on 8124');

Sources: - mongo connectivity code: https://github.com/christkv/node-mongodb-native/blob/master/examples/simple.js - node. http code: nodejs.org

EDIT CORRECTED CODE

Thanks to Mic below who got me rolling in the right direction. For anyone interested, the corrected solution is here:

function ReadTest(res){
    var db = new Db('mydb', new Server(host, port, {}), {native_parser:true});
    var result = "";
    res.write("in readtest\n");
    db.open(function (err, db) {
            res.write("now open\n");
            db.collection('test', function(err, collection) {
                    res.write("in collection\n");
                    collection.find(function (err, cursor){
                            res.write("found\n");
                            cursor.each( function (err, item) {
                                    res.write("now open\n");
                                    var x = PutItem(err, item);
                                    sys.puts(x);
                                    res.write(x);
                                    if (item == null) {
                                            res.end('foo');
                                    }
                            });
                    });
            });
    });
}

http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.write("start\n");
    ReadTest(res);
}).listen(8124);
console.log('Server running on 8124');
like image 299
Alex C Avatar asked Feb 24 '23 15:02

Alex C


2 Answers

My guess is that you are returning result, writing the response, and closing the connection before anything is fetched from the db.

One solution would be to pass the response object to where you actually need it, something like:

function readTest(res) {
    db.open(function (err, db) {
        db.collection('test', function(err, collection) {
            collection.find(function (err, cursor) {
                res.writeHead(200, {'Content-type' : 'text/plain'});
                cursor.each( function (err, item) { res.write(item); });
                res.end();
     ...

Of course, you should also handle errors and try to avoid nesting too many levels, but that's a different discussion.

like image 72
Mic Avatar answered Feb 27 '23 18:02

Mic


Instead of writing all the low-level Mongodb access code, you might want to try a simple library like mongous so that you can focus on your data, not on MongoDB quirks.

like image 36
Michael Dillon Avatar answered Feb 27 '23 18:02

Michael Dillon