Skip to content

Commit 5bec025

Browse files
committed
first draft
1 parent 2419c76 commit 5bec025

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed

snooty.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ php-library = "MongoDB PHP Library"
2424

2525
[constants]
2626
php-library = "MongoDB PHP Library"
27+
driver-short = "PHP library"

source/connect/client.txt

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
.. _php-client:
2+
3+
=======================
4+
Create a MongoDB Client
5+
=======================
6+
7+
.. facet::
8+
:name: genre
9+
:values: reference
10+
11+
.. meta::
12+
:keywords: connection string, URI, server, Atlas, settings
13+
14+
.. contents:: On this page
15+
:local:
16+
:backlinks: none
17+
:depth: 2
18+
:class: singlecol
19+
20+
Overview
21+
--------
22+
23+
To connect to a MongoDB deployment, you need two things:
24+
25+
- A **connection URI**, also known as a *connection string*, which tells the {+driver-short+}
26+
which MongoDB deployment to connect to.
27+
- A **MongoDB\Client** object, which creates the connection to the MongoDB deployment
28+
and lets you perform operations on it.
29+
30+
You can also use either of these components to customize the way the {+driver-short+} behaves
31+
while connected to MongoDB.
32+
33+
This guide shows you how to create a connection string and use a ``MongoDB\Client`` object
34+
to connect to MongoDB.
35+
36+
.. _php-connection-uri:
37+
38+
Connection URI
39+
--------------
40+
41+
A standard connection string includes the following components:
42+
43+
.. list-table::
44+
:widths: 20 80
45+
:header-rows: 1
46+
47+
* - Component
48+
- Description
49+
50+
* - ``mongodb://``
51+
52+
- Required. A prefix that identifies this as a string in the
53+
standard connection format.
54+
55+
* - ``username:password``
56+
57+
- Optional. Authentication credentials. If you include these, the client
58+
authenticates the user against the database specified in ``authSource``.
59+
For more information about the ``authSource`` connection option, see
60+
:ref:`php-auth`.
61+
62+
* - ``host[:port]``
63+
64+
- Required. The host and optional port number where MongoDB is running. If you don't
65+
include the port number, the driver uses the default port, ``27017``.
66+
67+
* - ``/defaultauthdb``
68+
69+
- Optional. The authentication database to use if the
70+
connection string includes ``username:password@``
71+
authentication credentials but not the ``authSource`` option. If you don't include
72+
this component, the client authenticates the user against the ``admin`` database.
73+
74+
* - ``?<options>``
75+
76+
- Optional. A query string that specifies connection-specific
77+
options as ``<name>=<value>`` pairs. See
78+
:ref:`php-connection-options` for a full description of
79+
these options.
80+
81+
For more information about creating a connection string, see
82+
:manual:`Connection Strings </reference/connection-string>` in the
83+
MongoDB Server documentation.
84+
85+
Create a ``MongoDB\Client``
86+
---------------------------
87+
88+
To create a connection to MongoDB, construct an instance of the ``MongoDB\Client`` class,
89+
passing the connection URI as a string to the constructor.
90+
91+
In the following example, the library uses a sample connection URI to connect to a MongoDB
92+
deployment on port ``27017`` of ``localhost``:
93+
94+
.. literalinclude:: /includes/connect/client.php
95+
:language: php
96+
:copyable: true
97+
98+
.. tip:: Reuse Your Client
99+
100+
Because each ``MongoDB\Client`` object represents a pool of connections to the
101+
database, most applications require only a single instance of
102+
``MongoDB\Client``, even across multiple requests.
103+
104+
API Documentation
105+
-----------------
106+
107+
To learn more about creating a ``MongoDB\Client`` object in the {+driver-short+},
108+
see the following API documentation:
109+
110+
- `:ref: MongoDB\Client <php-api-mongodbclient>`__

source/includes/connect/client.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
$client = new MongoDB\Client("mongodb://localhost:27017");

0 commit comments

Comments
 (0)