Fail to parse Mongo URI on connect #1347

doomhz opened this Issue Feb 16, 2013 · 0 comments


None yet
2 participants

doomhz commented Feb 16, 2013

It didn't work for me to pass a mongo_uri string to the Mongoose (3.5.6) connect method.

We recently switched JSLogger to MongoHQ and I had to specify a new remote Mongo connection URL (mongodb://<user>:<password>@<mongohqhost>:<mongohqport>/<database>) and connect with


Seems that Mongoose always falls back to the default Mongo port (27017). This is the error I got from Mongoose on connect:

        throw arguments[1]; // Unhandled 'error' event
Error: failed to connect to [jslogger:27017]
    at Server.connect.connectionPool.on.server._serverState (/Users/dumitru/RubymineProjects/
    at EventEmitter.emit (events.js:115:20)
    at connection.on._self._poolState (/Users/dumitru/RubymineProjects/
    at EventEmitter.emit (events.js:91:17)
    at Socket.errorHandler (/Users/dumitru/RubymineProjects/
    at Socket.EventEmitter.emit (events.js:88:17)
    at Socket.connect.require.lookup.addressType (net.js:697:16)
    at process.startup.processNextTick.process._tickCallback (node.js:244:9)

While looking into the Mongoose source at the = function (host, database, port, options, callback) function, I found out that the URL is not correctly parsed by the muri library. It always thinks that the Mongo user is the database name and the password is the port (that is non-numeric) and ignores it, falling back to the default Mongo port. That happens only when you have a # in your username or password :) Created an issue here:

An workaround is to specify your Mongo credentials as an object and call the mongoose.connect method with the native driver params:

    "mongo": {
      "host": "host",
      "port": "port",
      "user": "user",
      "pass": "pass",
      "db": "dbname"

and load them like this:

var fs = require('fs');
exports = mongoose = require('mongoose');
var environment = process.env.NODE_ENV || 'development';
var options = JSON.parse( fs.readFileSync(process.cwd()+'/config.json', encoding='utf8') )[environment];
var mongoUri = options.mongo_uri;
var mongo = options.mongo;
mongoUri ? mongoose.connect(mongoUri) : mongoose.connect(, mongo.db, mongo.port, {user: mongo.user, pass: mongo.pass});
exports = Schema = mongoose.Schema;

@aheckmann aheckmann added a commit that referenced this issue Feb 19, 2013

@aheckmann aheckmann updated; muri to 0.3.1
fixes #1347

aheckmann closed this in 6eca8e1 Feb 19, 2013

@aheckmann aheckmann added a commit that referenced this issue Mar 2, 2013

@aheckmann aheckmann updated; muri to 0.3.1
fixes #1347
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment