Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何在Mongo中查询?_Node.js_Mongodb - Fatal编程技术网

Node.js 如何在Mongo中查询?

Node.js 如何在Mongo中查询?,node.js,mongodb,Node.js,Mongodb,我正试图成功地询问Mongo,我的头撞在墙上 此代码: async.waterfall([ function(callback){ cursor = db.collection(collection).findOne(query) callback(null); }, function(callback){ console.log("Result is:" + curs

我正试图成功地询问Mongo,我的头撞在墙上

此代码:

    async.waterfall([
        function(callback){
            cursor = db.collection(collection).findOne(query)
            callback(null);
        },
        function(callback){
            console.log("Result is:" + cursor);
            console.log(JSON.stringify(cursor));
            callback(null);
        }
    ]);
生成以下输出:

result is:[object Object]
{}
为什么??有一个文档应该在集合中找到

作为后续问题,我如何才能看到什么

[object Object] 

是吗?

基本上,您应该等待查询完成,然后调用回调并期望得到任何结果:

        async.waterfall([
            function(callback){
                db.collection(collection).findOne(query, function(err, result) {
                  callback(err, result); // if there is no err, it will be null
                });
                // the above can be simplified to just                                    
                // db.collection(collection).findOne(query, callback);
                // since findOne callback and current function callback have the same arguments
            },
            function(result, callback) {
                // use comma here to automatically doing JSON.stringiry(result)
                console.log("Result is:", result);
                callback();
            }
        ], function(err) {
           // here is your final callback where you know that async.waterfall 
           // is finished (with or without error)
        });

基本上,您应该等待查询完成,然后调用回调并期望得到任何结果:

        async.waterfall([
            function(callback){
                db.collection(collection).findOne(query, function(err, result) {
                  callback(err, result); // if there is no err, it will be null
                });
                // the above can be simplified to just                                    
                // db.collection(collection).findOne(query, callback);
                // since findOne callback and current function callback have the same arguments
            },
            function(result, callback) {
                // use comma here to automatically doing JSON.stringiry(result)
                console.log("Result is:", result);
                callback();
            }
        ], function(err) {
           // here is your final callback where you know that async.waterfall 
           // is finished (with or without error)
        });

尝试
“结果是:”,光标
,而不是连接以正确记录。您应该将查询结果传递给回调,而不是游标。i、 e.使用
回调中的
回调
。findOne
。这是本地MongoDB驱动程序吗?什么版本?我用的是Mongo V3.0。请您用不同的词解释/说:“您应该将查询结果传递给回调,而不是游标。例如,在回调中使用回调到.findOne”。谢谢(旁白:“结果是:”,游标更改只打印{}。)尝试
“结果是:”,游标
,而不是连接以正确记录。您应该将查询结果传递给回调,而不是游标。i、 e.使用
回调中的
回调
。findOne
。这是本地MongoDB驱动程序吗?什么版本?我用的是Mongo V3.0。请您用不同的词解释/说:“您应该将查询结果传递给回调,而不是游标。例如,在回调中使用回调到.findOne”。谢谢(旁白:“结果是:”,光标更改只打印{}。)