MongoRegister()

Creates a new datasource for Mongo, to be referenced using the 'name' by the other Mongo functions. If already exists an exception will be thrown. If the connection has not been used within 5minutes it will be automatically closed and reopened on the next time it is called upon

Usage

BOOLEAN = MongoRegister( name, server, port, db, username, password, mongouri )
Argument Summary
name datasource name. Used to reference this connection
server the ip/network of the server
port network port of the server. defaults to 27017
db database name on Mongo [optional]
username username of the server [optional]
password password of the server [optional]
mongouri the mongo uri (see below). If used then server/port/db/username/password are ignored/ [optional]

Calling

Supports named-parameter calling allowing you to use the function like:

MongoRegister(
   name=?, 
   server=?, 
   port=?, 
   db=?, 
   username=?, 
   password=?, 
   mongouri=?
);

Supports passing parameters as a structure using ArgumentCollection:

MongoRegister( ArgumentCollection={
   name : ?, 
   server : ?, 
   port : ?, 
   db : ?, 
   username : ?, 
   password : ?, 
   mongouri : ?
} );

Extra

The format of the MongoURI is:

mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/database[?options]]
  • The database is required
  • mongodb:// is a required prefix to identify that this is a string in the standard connection format.
  • username:password@ are optional. If given, the driver will attempt to login to a database after connecting to a database server.
  • host1 is required part of the URI. It identifies a server address to connect to.
  • :portX is optional and defaults to :27017 if not provided.
  • /database is the name of the database to login to and thus is only relevant if the username:password@ syntax is used. If not specified the "admin" database will be used by default.
  • ?options are connection options. Note that if database is absent there is still a / required between the last host and the ? introducing the options. Options are name=value pairs and the pairs are separated by "&". For backwards compatibility, ";" is accepted as a separator in addition to "&", but should be considered as deprecated.

The Java driver supports the following options (case insensitive):

Replica set configuration:

  • replicaSet=name: Implies that the hosts given are a seed list, and the driver will attempt to find all members of the set.

Connection Configuration:

  • connectTimeoutMS=ms: How long a connection can take to be opened before timing out.
  • socketTimeoutMS=ms: How long a send or receive on a socket can take before timing out.

Connection pool configuration:

  • maxPoolSize=n: The maximum number of connections in the connection pool.
  • waitQueueMultiple=n: this multiplier, multiplied with the maxPoolSize setting, gives the maximum number of threads that may be waiting for a connection to become available from the pool. All further threads will get an exception right away.
  • waitQueueTimeoutMS=ms: The maximum wait time in milliseconds that a thread may wait for a connection to become available.

Write concern configuration:

  • safe=true|false
    • true: the driver sends a getLastError command after every update to ensure that the update succeeded.
    • false: the driver does not send a getLastError command after every update.
  • w=wValue
    • The driver adds { w : wValue } to the getLastError command. Implies safe=true.
    • wValue is typically a number, but can be any string in order to allow for specifications like "majority"
  • wtimeoutMS=ms
    • The driver adds { wtimeout : ms } to the getlasterror command. Implies safe=true.
    • Used in combination with w

Read preference configuration:

  • slaveOk=true|false: Whether a driver connected to a replica set will send reads to slaves/secondaries.
  • readPreference=enum: The read preference for this connection. If set, it overrides any slaveOk value.
    • Enumerated values:
      • primary
      • primaryPreferred
      • secondary
      • secondaryPreferred
      • nearest
  • readPreferenceTags=string. A representation of a tag set as a comma-separated list of colon-separated key-value pairs, e.g. "dc:ny,rack:1". Spaces are stripped from beginnin and end of all keys and values. To specify a list of tag sets, using multiple readPreferenceTags, e.g. readPreferenceTags=dc:ny,rack:1;readPreferenceTags=dc:ny;readPreferenceTags=
    • Note the empty value for the last one, which means match any secondary as a last resort.
    • Order matters when using multiple readPreferenceTags.

Reference MongoClientURI documentation.

Extra

For more information on using MongoDB, visit the MongoCFML page

For functions that update data, you can pass in the writeconcern. The possible values are:

  • FSYNC_SAFE
    Exceptions are raised for network issues, and server errors; the write operation waits for the server to flush the data to disk
  • JOURNAL_SAFE
    Exceptions are raised for network issues, and server errors; the write operation waits for the server to group commit to the journal file on disk
  • MAJORITY
    Exceptions are raised for network issues, and server errors; waits on a majority of servers for the write operation
  • NONE
    No exceptions are raised, even for network issues
  • NORMAL
    Exceptions are raised for network issues, but not server errors
  • REPLICAS_SAFE
    Exceptions are raised for network issues, and server errors; waits for at least 2 servers for the write operation
  • SAFE
    Exceptions are raised for network issues, and server errors; waits on a server for the write operation