Skip to content

Commit ad5b511

Browse files
mongoKartrustagir
andauthored
DOCSP-41957 - Mongo Client (#106)
Co-authored-by: Rea Rustagi <85902999+rustagir@users.noreply.github.com>
1 parent 474fc1f commit ad5b511

File tree

4 files changed

+113
-2
lines changed

4 files changed

+113
-2
lines changed

snooty.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@ php-library = "MongoDB PHP Library"
2525

2626
[constants]
2727
php-library = "MongoDB PHP Library"
28+
driver-short = "PHP library"
2829
mdb-server = "MongoDB Server"
29-
api = "https://www.mongodb.com/docs/php-library/current/reference"
30+
api = "https://www.mongodb.com/docs/php-library/current/reference"

source/connect/client.txt

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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 must create the following items:
24+
25+
- **Connection URI**, also known as a *connection string*, which tells the {+driver-short+}
26+
which MongoDB deployment to connect to.
27+
- **MongoDB\\Client** object, which creates the connection to the MongoDB deployment
28+
and lets you perform operations on it.
29+
30+
You can also set options within either or both of these components to
31+
customize the way that the {+driver-short+} behaves
32+
while connected to MongoDB.
33+
34+
This guide describes the components of a connection string and shows how to
35+
use a ``MongoDB\Client`` object to connect to a MongoDB deployment.
36+
37+
.. _php-connection-uri:
38+
39+
Connection URI
40+
--------------
41+
42+
A standard connection string includes the following components:
43+
44+
.. list-table::
45+
:widths: 20 80
46+
:header-rows: 1
47+
48+
* - Component
49+
- Description
50+
51+
* - ``mongodb://``
52+
53+
- Required. A prefix that identifies this as a string in the
54+
standard connection format.
55+
56+
* - ``db_username:db_password``
57+
58+
- Optional. Authentication credentials. If you include these, the client
59+
authenticates the user against the database specified in ``authSource``.
60+
For more information about the ``authSource`` connection option, see
61+
:ref:`php-auth`.
62+
63+
* - ``host[:port]``
64+
65+
- Required. The host and optional port number where MongoDB is running. If you don't
66+
include the port number, the driver uses the default port, ``27017``.
67+
68+
* - ``/defaultauthdb``
69+
70+
- Optional. The authentication database to use if the
71+
connection string includes ``db_username:db_password@``
72+
authentication credentials but not the ``authSource`` option. If you don't include
73+
this component, the client authenticates the user against the ``admin`` database.
74+
75+
* - ``?<options>``
76+
77+
- Optional. A query string that specifies connection-specific
78+
options as ``<name>=<value>`` pairs. See
79+
:ref:`php-connection-options` for a full description of
80+
these options.
81+
82+
To learn more about connection strings, see
83+
:manual:`Connection Strings </reference/connection-string>` in the
84+
Server manual.
85+
86+
Create a MongoDB\Client
87+
-----------------------
88+
89+
To create a connection to MongoDB, pass your connection string when constructing
90+
an instance of the ``MongoDB\Client`` class.
91+
92+
In the following example, the library uses a sample connection URI to connect to a MongoDB
93+
deployment on port ``27017`` of ``localhost``:
94+
95+
.. literalinclude:: /includes/connect/client.php
96+
:language: php
97+
:copyable: true
98+
99+
API Documentation
100+
-----------------
101+
102+
To learn more about creating a ``MongoDB\Client`` object in the {+driver-short+},
103+
see the following API documentation:
104+
105+
- :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");

source/reference/class/MongoDBClient.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.. _php-api-mongodbclient:
2+
13
=====================
24
MongoDB\\Client Class
35
=====================
@@ -62,4 +64,4 @@ Methods
6264
- :phpmethod:`MongoDB\Client::selectCollection()`
6365
- :phpmethod:`MongoDB\Client::selectDatabase()`
6466
- :phpmethod:`MongoDB\Client::startSession()`
65-
- :phpmethod:`MongoDB\Client::watch()`
67+
- :phpmethod:`MongoDB\Client::watch()`

0 commit comments

Comments
 (0)