Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I capture a MongoDB query as a string and display it in my Node JS page (using the mongojs driver)?

I would like to be able to query my mongoDB and display this result on my web page made with Node...right now I am using the mongojs driver - I've found the driver very very good for putting data into the DB - the syntax is the same as the Mongo shell and I can put the code right in my Node app. This task...simply showing the results of the query on the webpage, or even on the console, has proven very difficult. Here are the relevant parts of my code and what I tried.

var databaseUrl = "test"; // "username:[email protected]/mydb"
var collections = ["graph1"]
var db = require("mongojs").connect(databaseUrl, collections);

console.log(db.graph1.find());

I have made a collection called graph1 and in the mongo prompt this yields results. Note...I do want to display it in HTML...but I spose if I can get it to print to console I can get it in my HTML.

It currently outputs this:

{_oncursor: { get: [Function], put: [Function] } } 

Some kind of prototype for what I actually want, which is this:

{ "x" : "0", "y" : "1343725568", "_id" : ObjectId("4fba6....") }
like image 862
PinkElephantsOnParade Avatar asked May 21 '12 17:05

PinkElephantsOnParade


2 Answers

Try this:

    db.graph1.find( {}, function(err, result ){ 
    if (err || !result ) console.log(" an error has occurred" );
    else {
    console.log(result);
    }
    });

The console log that you had there was printing the db.graph1.find() return value which is its function prototype. It won't return anything useful because it's an asynchronous function. The only way to do useable things with the data it retrieves is to pass a callback in which will handle the data:

    db.graph1.find( { //what you want to search for here }, callback);

    function callback(result_from_mongo) {
    // do stuff here with result_from_mongo
    }
like image 96
Eoin Murray Avatar answered Oct 05 '22 01:10

Eoin Murray


For legacy sake, everyone should know further that the only time you can manipulate your query results is IN THE CALLBACK...so no setting it to a to a var to fool with afterwards, only within the callback )-=.

Use the following to make the query result a string without woes. It is standard library stuff.:

  var results_not_ugly_or_messed_up = (JSON.stringify(result));

If you want to be ghetto and use your results outside of the callback, you could always call a perl/python/sh/bat/whatever script with your "stringified" result (in this example,results_not_ugly_or_messed_up) as a parameter to store it in a file, etc., to read off and then use as you please.

For a full real-life example:

db.newguestbook.find({"Name" : /[Aa]/ },[],function(err,p) //newguestbook is a collection I have    
//you will need to change that unless you make a collection called newguestbook
{
    cursor = p;
    console.log(cursor);
    console.log(JSON.stringify(cursor))); //We have a nice string, see?
    var exec = require('child_process').exec;
    exec("perl writetofile.pl " + JSON.stringify(cursor) , function(err, 
    stdout, stderr)
    {
        console.log("Perl run to store this result in a file");
    });

});
}
like image 40
PinkElephantsOnParade Avatar answered Oct 05 '22 01:10

PinkElephantsOnParade