0

How can I connect without to a distant database (MONGOHQ) without using MongoClient.connect() ?

var db, mongo, server;

mongo = require("mongodb");

server = new mongo.Server("mongodb://login:[email protected]:10057//appname", 10057, {
 auto_reconnect: true
});

db = new mongo.Db("confirmed", server, { safe: true });

the message I get from my server is

[Error: failed to connect to [mongodb://login:[email protected]:10057//appname:10057]]

Any ideas ?

1
  • Are you able to connect from the command line mongo shell using the same credentials? Sample command line: mongo -u login -p password paulo.mongohq.com:10057/appname. It looks like you have an extra "/" before appname, and you have also duplicated the port number (10057). I suspect your Node.js connection string should be server = new mongo.Server("mongodb://login:[email protected]:10057/appname");. Commented Jan 10, 2014 at 2:06

1 Answer 1

1

You want something more like this, where you define the server as a DNS name (no protocol, port, auth or path):

server = new mongo.Server("paulo.mongohq.com", 10057, {
    auto_reconnect: true
});

db = new mongo.Db("confirmed", server, { safe: true });

and then once db is defined:

db.open(function(erreur, db) {
    db.authenticate('user', 'name', function(err, result) {
        //
    });
Sign up to request clarification or add additional context in comments.

2 Comments

The url to use would be paulo.mongohq.com/appname ?
@maximegir no, you'll need to specify the app name elsewhere; not too familiar with mongo, but if you look up the info in the docs (or even elsewhere on SO) you'll see that it connects to an IP address or DNS name, not a URL.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.