0

I've got a fairly "typical" setup (I'm using RailwayJS)

schema:

customSchema(function(){
    // Initialize Mongo DB
    var mongoose = require('mongoose'),
        Schema = mongoose.Schema;

    mongoose.connect('mongodb://localhost/mydb'); //todo: needs to come from connection or something?!

    var MyModel = new Schema({
        name : String,
        email : String,
        something : String
    });
});

My question is, how can I specify the connection string to use?

Should I move the mongoose.connect part out into my config section?

Alex
  • 37,502
  • 51
  • 204
  • 332

1 Answers1

0

This is what I like to use :

var mongourl = process.argv[2] || process.env.NODE_DB || 'mongodb://anywhere/db';

Remember to change NODE_DB to whatev er your env variable name is.

Note that I also allow to pass command line args (useful when using Cloud9 for running applications as this awesome service does not allow env variable). Index is 2 because the command is node app.js mongodb://address, remember to adjust if needed.

Arnaud Rinquin
  • 4,296
  • 1
  • 30
  • 51
  • Here is the [Heroku doc](https://devcenter.heroku.com/articles/nodejs#setting_node_env) on how to set up env variables (very easy). In your case just use `$ heroku config:add NODE_DB=mongodb://localhost/mydb` Note : if you're using Heroku, take a look at [MongoHQ add-on](https://addons.heroku.com/mongohq) so you can try a free, easy to setup and to maintain mongodb service. – Arnaud Rinquin Apr 17 '12 at 08:06