mongoose is an elegant mongodb object modeling for node.js

Mongoose Config

  1. Add mongoose to package.json

    package.json file add:

       "mongoose": "3.x.x",
  2. Update modules

     npm install
  3. Create a config file for global settings. Add a db key, with keys for production, development and test. Doing this allows us to set different databases for different environments.


     module.exports = {
       db: {
         production: "mongodb://",
         development: "mongodb://localhost:27017/storeski-dev",
         test: "mongodb://localhost:27017/storeski-test",
  4. Set the app.get('dbUrl') value to the db string value that corresponds to the environment we are using. E.g. if we are testing the app.settings.env will be 'test'. If we are in production the app.settings.env will be 'production'. Knowing this we can use this value to look up the db string that corresponds to the environment we are in. Add the following app.js:


     // module imports
     var config = require('./config');
     var mongoose = require('mongoose');
     app.configure(function () {
       // set the 'dbUrl' to the mongodb url that corresponds to the
       // environment we are in
       app.set('dbUrl', config.db[app.settings.env]);
       // connect mongoose to the mongo dbUrl