Connection String URI Format

Standard Connection String Format

This section describes the standard format of the MongoDB connection URI used to connect to a MongoDB database server. The format is the same for all official MongoDB drivers. For a list of drivers and links to driver documentation, see MongoDB Drivers and Client Libraries. The following is the standard URI connection scheme:

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

use MongoDB::Uri;
my MongoDB::Uri $uri-obj .= new(:uri<mongodb://localhost>);
ok $uri-obj.defined, 'T0';
is $uri-obj.server-data<servers>[0]<host>, "localhost", 'T1';
is $uri-obj.server-data<servers>[0]<port>, 27017, 'T2';
T0:
T1: default server name
T2: default port number
In the specification shown above it is necessary to specify at least one host and when there are more, all of these must be defined. In this driver implementation however, it is not necessary when host is localhost. So for this driver it is;

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

$uri-obj .= new(:uri<mongodb://>);
is $uri-obj.server-data<servers>[0]<host>, "localhost", 'T3';
is $uri-obj.server-data<servers>[0]<port>, 27017, 'T4';
$uri-obj .= new(:uri<mongodb://,,,>);
ok $uri-obj.server-data<servers>[1]<host>:!exists, 'T5';
T3: All is brought back to bare protocol specification and we get localhost.
T4: and its default port number 27017.
T5: No mather how many comma's are used to separate host specifications. So here there is no second host localhost:27017.

Username and password

$uri-obj .= new(:uri<mongodb://user:pencil@>);
is $uri-obj.server-data<username>, "user", 'T6';
is $uri-obj.server-data<password>, "pencil", 'T7';
dies-ok { $uri-obj .= new(:uri<mongodb://user:@>); }
, 'T8';
T6: Username found in uri is user.
T7: Password found in uri is pencil.
T8: It is not correct to specify the username without its password.

Database

$uri-obj .= new(:uri<mongodb://>);
is $uri-obj.server-data<database>, "admin", 'T9';
$uri-obj .= new(:uri<mongodb:///contacts>);
is $uri-obj.server-data<database>, "contacts", 'T10';
T9: Default database is admin.
T10: User defined database is contacts.

Options

$uri-obj .= new(:uri<mongodb://:65000/?replicaSet=test&authMechanism=SCRAM-SHA-1>);
is $uri-obj.server-data<options><replicaSet>, "test", 'T11';
is $uri-obj.server-data<options><authMechanism>, "SCRAM-SHA-1", 'T12';
T11: Option replicaSet is test.
T12: Option authMechanism is SCRAM-SHA-1.
Normal testsBug issuesTodo testsSummary
1301300000013ok0Tests0Bugs0Todo0Skip13Total