MongoDB standard connection string
MongoDB ://[Username: password @] host1 [: port1] [, host2 [: port2],… [, Hostn [: portn] [/[database] [? Options]
Note: not all MongoDB drivers support the complete connection string. drivers that do not support this format will have an alternative connection scheme. For more information, see the instructions of the driver.
MongoDB: // is the prefix string required for the connection string
Username: password @ optional. After you connect to the database, you will try to verify the login
Host1 must specify at least one host
: Portx option. The default value is 27017.
/Database if Username: password @ is specified, connect to and verify the login to the specified database. If this parameter is not specified, the admin database is enabled by default.
? Options is the connection option. If you do not use the/database, you must add /. All connection options are key-value pairs name = value. Key-value pairs are separated by & or; (semicolon ).
Connection options include:
Replica set:
Replicaset = Name
The driver verifies the name of the replica set. This means that the given hosts is the master database (seed list), and the driver will try to find all the members in the replica set. (• 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 .)
Single Server:
Slaveok = true | false
Free options:
Safe = true | false
True: DriverProgramAfter each update operation is submitted, the getlasterror command is executed to confirm that the update is valid (see w and wtimeoutms)
False: the driver does not execute getlasterror after each update operation.
W = N
The driver adds the {W: n} parameter to the getlasterror command. Meaning safe = true
Wtimeoutms = MS
The driver added the {wtimeout: MS} parameter to the getlasterror command. It means safe = true.
Fsync = true | false
True: The driver adds the {fsync: true} parameter to the getlasterror command. It means safe = true.
False: the driver does not add the fsync parameter to the getlasterror command.
Journal = true | false
True: Synchronize to journal. Meaning safe = true.
Connecttimeoutms = MS
Set connection establishment timeout, in MS
Sockettimeoutms = MS
Set the timeout time for sending or receiving a socket, in ms.
These options are case-insensitive.
Connect to MongoDB (connect to localhost: 27017 by default)
Use user Fred and password foobar to connect
MongoDB: // FRED: foobar @ localhost |
Use the user Fred and password foobar to connect and specify the database Baz
MongoDB: // FRED: foobar @ localhost/Baz |
Connect to the replica sets composed of two servers
Apsaradb for MongoDB: // example.com: 27017, example2.com: 27017 |
Connect to the replica sets composed of three local servers (use ports 27017, 27018, and 27019 respectively)
MongoDB: // localhost, localhost: 27018, localhost: 27019 |
Connect to the replica sets composed of three servers, and centralize all write operations in the master database. Read operations are distributed in each cluster.
MongoDB: // host1, host2, host3 /? Slaveok = true |
Connect in Safe Mode
MongoDB: // localhost /? Safe = true |
Connect to a group of replica sets in safe mode, wait until at least two machines are synchronized successfully, and set the timeout time of two seconds.
MongoDB: // host1, host2, host3 /? Safe = true; W = 2; wtimeoutms = 2000 |
Connection Pool (Connection pooling)
Each TCP connection of the server corresponds to a process. We strongly recommend that you implement your own connection pool in applications. Most drivers will also quietly help you with the connection pool. A common exception is that your application will reconfigure a new process such as CGI and PHP for each request.