NodeJS - MongoDB

nodejs

mongoose

http://mongodb.org/
http://docs.mongodb.org/manual/installation/
http://webapplog.com/mongoui/comment-page-1
http://webapplog.com/mongoui/
http://webapplog.com/mongoose-course-node-and-mongo-explained/
http://webapplog.com/mongoose-guide/
http://webapplog.com/express-js-and-mongoose-example-building-hackhall/
http://code.tutsplus.com/tutorials/build-a-complete-mvc-website-with-expressjs--net-34168 - done reading
https://github.com/etabits/node-penguin
https://coderwall.com/p/v-ifpq/build-your-mongodb-admin-panel-in-minutes
http://www.sitepoint.com/creating-crud-app-minutes-angulars-resource/

What is MongoDB?

Mongo is a NoSQL document database. Once you finish with the installation, run the MongoDB daemon, which by default listens on port 27017. So, in theory, we should be able to connect to this port and communicate with the mongodb server.

How can we test connecting to the MongoDB database?

Next, we are going to write a test, which checks if there is a mongodb server running. /tests/mongodb.spec.js file:

describe("MongoDB", function() {
    it("is there a server running", function(next) {
        var MongoClient = require('mongodb').MongoClient;
        MongoClient.connect('mongodb://127.0.0.1:27017/fastdelivery', function(err, db) {
            expect(err).toBe(null);
            next();
        });
    });
});

The callback in the .connect method of the mongodb client receives a db object. We will use it later to manage our data, which means that we need access to it inside our models. It's not a good idea to create a new MongoClient object every time when we have to make a request to the database. That's why I moved the running of the express server inside the callback of the connect function:

MongoClient.connect('mongodb://127.0.0.1:27017/fastdelivery', function(err, db) {
    if(err) {
        console.log('Sorry, there is no mongo db server running.');
    } else {
        var attachDB = function(req, res, next) {
            req.db = db;
            next();
        };
        http.createServer(app).listen(config.port, function(){
            console.log('Express server listening on port ' + config.port);
        });
    }
});

Even better, since we have a configuration setup, it would be a good idea to place the mongodb host and port in there and then change the connect URL to:

'mongodb://' + config.mongo.host + ':' + config.mongo.port + '/fastde

Pay close attention to the middleware: attachDB, which I added just before the call to the http.createServer function. Thanks to this little addition, we will populate a .db property of the request object. The good news is that we can attach several functions during the route definition. For example:

app.get('/', attachDB, function(req, res, next) {
    ...
})

So with that, Express calls attachDB beforehand to reach our route handler. Once this happens, the request object will have the .db property and we can use it to access the database.

How to connect to mongodb?

MongoClient.connect('mongodb://127.0.0.1:27017/fastdelivery', function(err, db) {
    if(err) {
        console.log('Sorryere is no mongo db server running.');
    } else {
        var attachDB = function(req, res, next) {
            req.db = db;
            next();
        };
        http.createServer(app).listen(config.port, function(){
            console.log('Expressver listening on port ' + config.port);
        });
    }
});

Even better, since we have a configuration setup, it would be a good idea to place the mongodb host and port in there and then change the connect URL to:

'mongodb://' + config.mongo.host + ':' + config.mongo.port + '/fastdelivery'

Pay close attention to the middleware: attachDB, which I added just before the call to the http.createServer function. Thanks to this little addition, we will populate a .db property of the request object. The good news is that we can attach several functions during the route definition. For example:

app.get('/', attachDB, function(req, res, next) {
    ...
})

So with that, Express calls attachDB beforehand to reach our route handler. Once this happens, the request object will have the .db property and we can use it to access the database.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License