The core issue seems to be that you're trying to use a MongoDB URI as a hostname.
Here's how to connect using a URI and MongoClient
:
var mongodb = require('mongodb');
var uri = 'mongodb://user:pass@host:port/db';
mongodb.MongoClient.connect(uri, function (err, db) {
/* adventure! */
});
Of course you'll want to substitute the user
, pass
, host
, port
, and db
in the uri
for your actual connect parameters. If you're using the MongoLab add-on for Heroku you can get the URI from the environment like this:
var uri = process.env.MONGOLAB_URI;
When using MongoClient
safe mode is the default, so that option can be left out. To specify auto_reconnect
simply pass it as a server option.
var mongodb = require('mongodb');
var uri = 'mongodb://user:pass@host:port/db';
mongodb.MongoClient.connect(uri, { server: { auto_reconnect: true } }, function (err, db) {
/* adventure! */
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…