Skip to content

1A. Advanced Connections

Jon Clausen edited this page Dec 21, 2015 · 5 revisions

Advanced Connection Configuration Examples

Single Server Connection Using Authentication:

NOTE: Some third-party providers (e.g. MongoLab) use the connnection database as the authentication database. Nine times out of ten, however, the authenticationDB value will be "admin". If you omit that host key, the connection will default to "admin".*

MongoDB = {
	//an array of servers to connect to
	hosts= [

		{
			serverName='ds012345.mongolab.com',
			serverPort='12345',
			//Note that auth credentials are required for the first server only.  All other instances assume duplicate credentials
			username="myUsername",
			password="my53cUr3P455",
			authenticationDB="myremotedb"
		}
		
	  ],
  	//the default client options
	clientOptions = {
		//The connection timeout in ms (if omitted, the timeout is 30000ms)
		"connectTimeout":2000
	},

  	//The default database to connect to
	db 	= "mydbname",
	//whether to permit viewing of the API documentation
	permitDocs = true,
	//whether to permit unit tests to run
	permitTests = true,
	//whether to permit generic API
	permitAPI = true
};

*If using a connection which authenticates against an admin database, MongoDB will create your database if it doesn't exist automatically, so you can use any name you choose for your database (or collections) from the get-go.

Multiple Servers With Authentication and Advanced Options

Multiple servers may be used by including those servers in the connection hosts array. Advanced connection options may be specified, including readPreference and writeConcern. See the MongoClientOptions Builder documentation for details on available options. If an option is specified which is not available, the Config object will throw an error.

MongoDB = {
	//an array of servers to connect to
	hosts= [

		{
			serverName='ds012345.mongolab.com',
			serverPort='12345',
			username="myUsername",
			password="my53cUr3P455",
			authenticationDB="myremotedb"
		},
		//note that we only needed the credentials and the authenticationDB value for the first host entry.  
        //All other hosts must use the same values.
		{
			serverName='ds023456.mongolab.com',
			serverPort='23456',
		},
		{
			serverName='ds034567.mongolab.com',
			serverPort='34567',
		}
		
	  ],
  	//the default client options
	clientOptions = {
		//The connection timeout in ms (if omitted, the timeout is 30000ms)
		"connectTimeout":2000,
		//Set a miniumum of 10 connections to keep open - our driver will use all of these connections in its pool
		"minConnectionsPerHost":10,
		//Sets our read preference to read from a secondary, allowing the primary to handle our writes
		"readPreference":"secondaryPreferred"
		//Sets our write concern to wait for acknowledgement from at least two of the replica set hosts
		"writeConcern":"W2",
		//Send keepalives to all connections
		"socketKeepAlive":true	
	},

  	//The default database to connect to
	db 	= "mydbname",
	//turn off our documentation in production
	permitDocs = false,
	//turn off tests in production
	permitTests = false,
	//turn off Generic API
	permitAPI = false
};