Skip to content

Instantly share code, notes, and snippets.

@amlwwalker
Created May 11, 2018 13:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save amlwwalker/09e989e5ee9da889e5514c120fa3e639 to your computer and use it in GitHub Desktop.
Save amlwwalker/09e989e5ee9da889e5514c120fa3e639 to your computer and use it in GitHub Desktop.
Configuration I have been using for connecting to MongoDB Atlas
ENVIRONMENT=development
HOSTNAME=localhost:4000
MONGODB_DB=reactnodenew
MONGODB_HOST=localhost
MONGODB_PASS=
MONGODB_PORT=27017
MONGODB_QUERIES=
MONGODB_SSL=FALSE
MONGODB_USER=
NODE_ENV=development
require('dotenv').config({ path: './.localenv' })
var config = {}
config.hostname = process.env.HOSTNAME || 'localhost:3000'
config.mongodb = {}
config.mongodb.host = process.env.MONGODB_HOST || '127.0.0.1'
config.mongodb.port = process.env.MONGODB_PORT || 27017
config.mongodb.user = process.env.MONGODB_USER || ''
config.mongodb.password = process.env.MONGODB_PASS || ''
config.mongodb.database = 'react'
config.mongodb.queries = process.env.MONGODB_QUERIES || ''
//define the mongo address
config.mongodb.uri = 'mongodb://'
if (config.mongodb.user.length && config.mongodb.password.length)
config.mongodb.uri +=
config.mongodb.user + ':' + config.mongodb.password + '@'
config.mongodb.uri += config.mongodb.host
if (config.mongodb.port.toString().length)
config.mongodb.uri += ':' + config.mongodb.port.toString()
if (config.mongodb.database.length)
config.mongodb.uri += '/' + config.mongodb.database
if (process.env.MONGODB_SSL == 'TRUE') {
config.mongodb.ssl = true
config.mongodb.uri += '?ssl=true'
} else {
config.mongodb.ssl = false
}
//inside models directory (models/db.js
'use strict';
var config = require('../config');
var mongoose = require('mongoose');
mongoose.Promise = global.Promise;
var connection;
var connect = function() {
console.log("connecting over SSL? ", config.mongodb.ssl)
var options = {
server: {
ssl: config.mongodb.ssl,
auto_reconnect: true,
replset: {
socketOptions: {
connectTimeoutMS: 120000,
socketTimeoutMS: 120000,
keepAlive: 1
}
},
socketOptions: {
connectTimeoutMS: 120000,
socketTimeoutMS: 120000,
keepAlive: 1
}
}
}
if (config.mongodb.queries != "") {
config.mongodb.queries = "&" + config.mongodb.queries
}
mongoose.connect(config.mongodb.uri + config.mongodb.queries, options);
console.log("FINAL: " + config.mongodb.uri + config.mongodb.queries)
};
connect();
mongoose.connection.on('error', console.log);
mongoose.connection.on('disconnected', connect);
module.exports.getConnection = function() {
return mongoose.connection;
}
@amlwwalker
Copy link
Author

amlwwalker commented May 11, 2018

I get the config from mongo atlast and re build the connection string.
Setting MONGODB_HOST to something like: MONGODB_HOST=xx-shard-00-00-xx.mongodb.net:27017,xx-shard-00-01-xx.mongodb.net:27017,xx-shard-00-02-xx.mongodb.net

Notice the last shard does not have the port on the end as for localhost need to add it on later

Notice the MONGODB_QUERIES is a seperate env var b/c you don't need it when working locally

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment