Skip to content

Commit 3a9c15e

Browse files
alcaeusjmikola
andauthored
PHPLIB-596: Add tutorial for connecting to MongoDB (#979)
* PHPLIB-596: Add tutorial for connecting to MongoDB * Fix formatting breaking TOC Co-authored-by: Jeremy Mikola <[email protected]> * Link connecting tutorial on tutorials page Co-authored-by: Jeremy Mikola <[email protected]>
1 parent 248e4e5 commit 3a9c15e

File tree

4 files changed

+48
-3
lines changed

4 files changed

+48
-3
lines changed

docs/index.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ following pages should help you get started:
2727

2828
- :doc:`/tutorial/install-php-library`
2929

30+
- :doc:`/tutorial/connecting`
31+
3032
- :doc:`/tutorial/crud`
3133

3234
- :doc:`/tutorial/commands`

docs/reference/method/MongoDBClient__construct.txt

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,27 @@ initialized on demand, when the first operation is executed.
4949
Examples
5050
--------
5151

52+
.. start-connecting-include
53+
54+
Connecting to a Standalone server
55+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
56+
57+
If you do not specify a ``$uri`` value, the driver connects to a standalone
58+
:program:`mongod` on ``127.0.0.1`` via port ``27017``. To connect to a different
59+
server, pass the corresponding connection string as the first parameter when
60+
creating the :phpclass:`Client <MongoDB\\Client>` instance:
61+
62+
.. code-block:: php
63+
64+
<?php
65+
66+
$client = new MongoDB\Client('mongodb://mongodb-deployment:27017');
67+
5268
Connecting to a Replica Set
5369
~~~~~~~~~~~~~~~~~~~~~~~~~~~
5470

55-
If you do not specify a ``$uri`` value, the driver connects to a standalone
56-
:program:`mongod` on ``127.0.0.1`` via port ``27017``. The following example
57-
demonstrates how to connect to a replica set with a custom read preference:
71+
The following example demonstrates how to connect to a replica set with a custom
72+
read preference:
5873

5974
.. code-block:: php
6075

@@ -106,6 +121,8 @@ which may be specified in the constructor's ``$driverOptions`` parameter. Those
106121
options are covered in the :php:`MongoDB\\Driver\\Manager::__construct()
107122
<mongodb-driver-manager.construct>` documentation.
108123

124+
.. end-connecting-include
125+
109126
Specifying a Custom Type Map
110127
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
111128

docs/tutorial.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Tutorials
55

66
.. toctree::
77

8+
/tutorial/connecting
89
/tutorial/crud
910
/tutorial/collation
1011
/tutorial/commands

docs/tutorial/connecting.txt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
=====================
2+
Connecting to MongoDB
3+
=====================
4+
5+
.. default-domain:: mongodb
6+
7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 2
11+
:class: singlecol
12+
13+
Creating a Client instance
14+
--------------------------------------------------------
15+
16+
.. include:: /reference/method/MongoDBClient__construct.txt
17+
:start-after: start-connecting-include
18+
:end-before: end-connecting-include
19+
20+
Specifying connection options
21+
-----------------------------
22+
23+
Connection options can be passed via the ``$uri`` parameter, or through the
24+
``$options`` and ``$driverOptions`` parameters. The available options are
25+
documented in the :phpmethod:`MongoDB\\Client::__construct()` reference.

0 commit comments

Comments
 (0)