How do I connect mongodb with Node.js correctly?

Hi I've been trying to get this code to work but I could not figure out what's wrong with my code. It should have printed "here..." if the connection is open. Also, I look at the console of "mongod" it shows there's a connection opened but nothing prints out.


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. 


Also when I check there's no database created as well. I thought that mongodb database is lazily created once there's something inserted?

Thanks a lot.

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! :)