MongoDB DB connections

1.   The standard connection uri scheme is not yet supported by all of the drivers. All drivers support an alternative method of specifying connections if this format is not supported.

 

2.   The standard connection string format is as below:

mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]

 

  a)   mongodb:// is a required prefix to identify that this is a string in the standard connection format.

  b)   username:password@ are optional. If given, the driver will attempt to login to a database after connecting to a database server.

  c)   host1 is the only required part of the URI. It identifies a server address to connect to. As many hosts as necessary may be specified (for connecting to replica pairs/sets).

  d)   :portX is optional and defaults to :27017 if not provided.

  e)   /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.

  f)   ?options are connection options. Note that if database is absent there is still a / required between the last host and the ? introducing the options. 

 

3.  Options are name=value pairs and the pairs are separated either by "& "" or "; ". (These options are not case sensitive):

  a)   replicaSet=name

The driver verifies that the name of the replica set it connects to matches this name. Implies that the hosts given are a seed list, and the driver will attempt to find all members of the set.

  b)   slaveOk=true|false

  true : sending all writes to the primary and distributing reads to the secondaries. false :all writes and reads are sent to primary.

  c)   safe=true|false

true : the driver sends a getLastError command after every update to ensure that the update succeeded.

  d)   w=n

The driver adds { w : n } to the getLastError command. Implies safe=true . Updates will wait for replication to succeed on at least n machines.

  e)   wtimeoutMS=ms

The driver adds { wtimeout : ms } to the getLastError command. Implies safe=true . The timeout for waiting on replication success.

  f)   fsync=true|false

true : the driver adds { fsync : true } to the getLastError command. Implies safe=true . Force MongoDB to flush all pending writes to the data files.

  g)   journal=true|false

true : Sync to journal. Implies safe=true .

  h)   connectTimeoutMS=ms

How long a connection can take to be opened before timing out.

  i)   socketTimeoutMS=ms

How long a send or receive on a socket can take before timing out.

 

4.   The server will use one thread per TCP connection, therefore it is highly recomended that your application use some sort of connection pooling. Luckily, most drivers handle this for you behind the scenes.

猜你喜欢

转载自seanzhou.iteye.com/blog/1573251
今日推荐