无法使用node.js和MongoDB包查询MongoDB数据库

无法使用node.js和MongoDB包查询MongoDB数据库,node.js,mongodb,npm,Node.js,Mongodb,Npm,我无法使用node.js和MongoDB包查询我的MongoDB数据库 我成功地插入了两个对象,如下所示: var mongo = require("mongodb"); var host = "127.0.0.1"; var port = mongo.Connection.DEFAULT_PORT; var db = new mongo.Db("nodejs-introduction", new mongo.Server(host, port, { }), {safe: true}); con

我无法使用node.js和MongoDB包查询我的MongoDB数据库

我成功地插入了两个对象,如下所示:

var mongo = require("mongodb");
var host = "127.0.0.1";
var port = mongo.Connection.DEFAULT_PORT;
var db = new mongo.Db("nodejs-introduction", new mongo.Server(host, port, { }), {safe: true});
console.log(host);
db.open(function(error) {
    console.log("We are connected ", + host + ':' + port);

    db.collection("user", { w:1 }, function(error, collection){
        console.log("We have the collection");

        collection.insert({
            id: "1",
            name: "Foo Bar",
            twitter: "Foo_Bar",
            email: "info@nfoobar.com"
        }, {w:1}, function() {
            console.log("Succesfully inserted Foo Bar");
        });

        collection.insert({
            id: "2",
            name: "Bar Foo",
            twitter: "Bar_Foo",
            email: "info@tbarfoo.com"
        }, {w:1}, function() {
            console.log("Succesfully inserted Bar Foo");
        });
    });
});
var mongo = require("mongodb");
var host = "127.0.0.1";
var port = mongo.Connection.DEFAULT_PORT;
var db = new mongo.Db("nodejs-introduction", new mongo.Server(host, port, { }), {safe: false});
console.log(host);
db.open(function(error) {
    console.log("We are connected ", + host + ':' + port);

    //collection is sort of table
    db.collection("user", function(error, collection){
        console.log("We have the collection");
        collection.find({ "id": "1" }, function(error, cursor){
            cursor.toArray(function(error, users){
                if (users.length == 0) {
                    console.log("no user found");
                } else {
                    console.log("Found a user", users[0]); 
                }
            });
        });

    });
});
这里一切顺利,没有错误。但如果我想这样查询项目:

var mongo = require("mongodb");
var host = "127.0.0.1";
var port = mongo.Connection.DEFAULT_PORT;
var db = new mongo.Db("nodejs-introduction", new mongo.Server(host, port, { }), {safe: true});
console.log(host);
db.open(function(error) {
    console.log("We are connected ", + host + ':' + port);

    db.collection("user", { w:1 }, function(error, collection){
        console.log("We have the collection");

        collection.insert({
            id: "1",
            name: "Foo Bar",
            twitter: "Foo_Bar",
            email: "info@nfoobar.com"
        }, {w:1}, function() {
            console.log("Succesfully inserted Foo Bar");
        });

        collection.insert({
            id: "2",
            name: "Bar Foo",
            twitter: "Bar_Foo",
            email: "info@tbarfoo.com"
        }, {w:1}, function() {
            console.log("Succesfully inserted Bar Foo");
        });
    });
});
var mongo = require("mongodb");
var host = "127.0.0.1";
var port = mongo.Connection.DEFAULT_PORT;
var db = new mongo.Db("nodejs-introduction", new mongo.Server(host, port, { }), {safe: false});
console.log(host);
db.open(function(error) {
    console.log("We are connected ", + host + ':' + port);

    //collection is sort of table
    db.collection("user", function(error, collection){
        console.log("We have the collection");
        collection.find({ "id": "1" }, function(error, cursor){
            cursor.toArray(function(error, users){
                if (users.length == 0) {
                    console.log("no user found");
                } else {
                    console.log("Found a user", users[0]); 
                }
            });
        });

    });
});
然后我得到以下错误:

127.0.0.1
We are connected  NaN:27017
We have the collection

/path/to/query.js:14
                if (users.length == 0) {
                         ^
TypeError: Cannot read property 'length' of null

所以我理解的是变量users为null,它找不到任何匹配项,但我不知道为什么。我做错了什么?

找到了解决办法。Mongodb没有运行。如果MongoDB没有运行,我怎么能在没有任何错误的情况下插入MongoDB?

文档中可能没有元素
users
,因此返回
null
。您必须检查
null
:如果(users==null){…}否则…没有匹配项,因此它返回
null
,而不是一个空的
数组
。我理解这一点,但我不理解的是为什么没有匹配项。请尝试从mongo shell进行查询。