0

嗨,我一直试图让这段代码工作,但我无法弄清楚我的代码有什么问题。如果连接打开,它应该打印“here...”。另外,我查看了“mongod”的控制台,它显示有一个连接打开,但没有打印出来。


var Db = require('mongodb').Db;
var Server = require('mongodb').Server;
var client = new Db('test1', new Server('127.0.0.1', 27017, {}));


var Vocabulary = function() {

    function get(german_vocab) {
        client.open(function(err, pClient) {
            console.log("here...")
            client.collection('test1', function(err, collection) {
                collection.insert({name:"myself"});
            });

            client.collection.find().toArray(function(err, results) {
                console.log(results);
            });
        });

    }

    return {
        get : get
    };
}

module.exports = Vocabulary;


var vocab = Vocabulary();
vocab.get("Ich"); // Nothing shows in this line. 


此外,当我检查没有创建数据库时。我认为一旦插入了一些东西,mongodb 数据库就会被懒惰地创建?

非常感谢。

4

1 回答 1

0

In the docs they describe a function called db.createCollection([[name[, options]], callback), have you tried that? And maybe you should think about using something like mongoose, which is a nice 3rd party lib for working with mongodb.

I hope I could help! :)

于 2012-05-13T20:58:13.097 回答