如何将MongoDB查询捕获为字符串并将其显示在我的Node JS页面中(使用mongojs驱动程序)?

时间:2022-11-15 03:04:01

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.

我希望能够查询我的mongoDB并在我的网页上显示这个结果...现在我正在使用mongojs驱动程序 - 我发现驱动程序非常适合将数据放入数据库 - 语法与Mongo shell相同,我可以将代码放在我的Node应用程序中。这个任务......简单地在网页上甚至在控制台上显示查询结果已经证明非常困难。以下是我的代码的相关部分以及我尝试过的内容。

var databaseUrl = "test"; // "username:password@example.com/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.

我创建了一个名为graph1的集合,并在mongo提示符中生成结果。注意......我确实希望以HTML格式显示它...但是如果我可以将它打印到控制台我可以使用我的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....") }

2 个解决方案

#1


3  

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()返回值,这是它的函数原型。它不会返回任何有用的东西,因为它是一个异步函数。使用它检索的数据做可用事物的唯一方法是传递一个回调函数来处理数据:

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

    function callback(result_from_mongo) {
    // do stuff here with result_from_mongo
    }

#2


1  

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.

如果你想成为贫民窟并在回调之外使用你的结果,你总是可以用你的“字符串化”结果(在这个例子中,results_not_ugly_or_messed_up)作为参数来调用perl / python / sh / bat /任何脚本来存储它一个文件等,以便读取然后随意使用。

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");
    });

});
}

#1


3  

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()返回值,这是它的函数原型。它不会返回任何有用的东西,因为它是一个异步函数。使用它检索的数据做可用事物的唯一方法是传递一个回调函数来处理数据:

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

    function callback(result_from_mongo) {
    // do stuff here with result_from_mongo
    }

#2


1  

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.

如果你想成为贫民窟并在回调之外使用你的结果,你总是可以用你的“字符串化”结果(在这个例子中,results_not_ugly_or_messed_up)作为参数来调用perl / python / sh / bat /任何脚本来存储它一个文件等,以便读取然后随意使用。

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");
    });

});
}