Node.js MongoDB查询没有返回结果。

13

我正在尝试使用 MongoDB,并向“users”集合中输入了一些测试数据 {name:"david"}。通过在 mongo shell 中输入,我验证了数据已经存入 MongoDB:

db.users.find()

结果:

{ "name":"david" }

在node.js脚本中,以下代码:

db.open(function(err, db) {
    if (!err) {
        console.log("db opened!");
    }
    else {
        console.log(err);
    }
    db.collection('users', function(err, collection) {
        collection.find({}, function(err, cursor) {
            cursor.each(function(err, item) {
                console.log(item);
            });
        });
    });
    db.close();
});

没有返回任何结果

我没有看到任何问题,也没有错误返回。请给予建议。

4个回答

10

实际上,你在数据集返回之前就关闭了数据库连接。

db.collection('users', function(err, collection) {
  collection.find({}, function(err, cursor) {
    cursor.each(function(err, item) {
      console.log(item);
    });

    // our collection has returned, now we can close the database
    db.close();
  });
});

4

正如CJohn正确指出的那样,您在检索数据之前关闭了DB连接。我知道这看起来不像是这种情况,但这是Node结构和回调的情况。正确处理此问题的代码如下:

db.open(function(err, db) {
    if (err) return console.log('error opening db, err = ', err);

    console.log("db opened!");

    db.collection('users', function(err, collection) {
        if (err) return console.log('error opening users collection, err = ', err);

        collection.find({}, function(err, cursor) {
            if (err) return console.log('error initiating find on users, err = ', err);

            cursor.each(function(err, item) {
                // watch for both errors and the end of the data
                if (err || ! item) {
                    // display (or do something more interesting) with the error
                    if (err) console.log('error walking data, err = ', err);

                    // close the connection when done OR on error
                    db.close();

                    return;
                }
                console.log(item);
            });
        });
    });
});

3
这个模式在我的node/mongo示例中运行良好。该函数会传递一个回调,其中包含err和collection参数。它获取'users'集合,如果成功则针对该集合调用find方法并将其转换为数组,但您也可以迭代游标。
在db.collection和connection.find调用中,您没有检查err和处理。您只在open调用上进行了处理。
此外,您不应该调用db.close(),特别是如果您使用了connection pool option(您不想在每次调用时打开和关闭连接)。如果您确实想关闭,请在回调内关闭。
例如:
var server = new Server(host, port, {auto_reconnect: true, poolSize: 5}, {});

MyStore.prototype.getUsers = function(callback) {
server.open(function(err, db) {
    if (err) {
        callback(err);
    }
    else {
        db.collection('users', function(err, collection) {
            if( err )
                callback(err);
            else {
                collection.find().toArray(function(err, users) {
                    if (err) {
                        callback(err)
                    } else {
                        callback(null, users);
                    }
                });
            }
        }
    }});

这里有另一个关于node和mongo的教程,可能会有所帮助:http://howtonode.org/express-mongodb


3
尝试将您的节点升级到最新版本。
sudo npm cache clean -f
sudo npm install -g n
sudo n stable

版本0.4可能无法正常工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接