How to connect MongoHQ to AppFog mongodb service

386 views Asked by At

js with express.js app, i deployed it to appfog but i´m not able or i don´t know how to connect my mongodb collections located on MongoHQ to my AppFog mongodb service.

MongoHQ provides you a mongo URI like:

mongodb://<user>:<password>@<hostname>:<port>/<databasename>

AppFog mongodb service provides you a environment variable called VCAP_SERVICE that contains all conf variables to connect your app to mongodb, but in the other hand i have my mongodb collections located on MongoHQ and i don´t know how to specify this MongoURI to my mongodb service on AppFog.

Hope you understand me, regards.

1

There are 1 answers

2
afterglowlee On

If I understand correctly, there might be two ways to solve your problem. Firstly, you may enable the MongoHQ add-on and AppFog will create a MongoHQ sandbox for you. You can get the MongoHQ URL by:

if(process.env.VCAP_SERVICES){
    var env = JSON.parse(process.env.VCAP_SERVICES);
    var mongo = env['MONGOHQ_URL'];
}

Since you said you have already got mongodb collections, you can clone your original collections to the one offered by AppFog via MongoHQ Web UI.

The other solution is don't worry about the MongoHQ add-on in AppFog. Just directly connect to MongoHQ (I use mongoose, but native driver should do the similar thing):

mongoose.connect(YOUR_MONGOHQ_URL);

Actually I got a similar problem recently and I successfully deploy my nodejs+express app to Heroku using this method. I was not bothered to enable the mongohq add-on in Heroku since I have already got a MongoDB sandbox.

UPDATE I recently deployed a couple of applications in Heroku using mongodb native driver and monk to connect to a sandbox in MongoHQ. The code looks like:

var monk=require('monk');
var db=monk("mongodb://username:[email protected]:port/YOURDB");