Skip to content

PHPLIB-596: Add tutorial for connecting to MongoDB #979

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/index.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ following pages should help you get started:

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

- :doc:`/tutorial/connecting`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's also add /tutorial/connecting to the toctree in tutorial.txt to ensure this page is surfaced there as well.

Tutorials
=========

.. default-domain:: mongodb

.. toctree::

+  /tutorial/connecting
   /tutorial/crud
   /tutorial/collation


- :doc:`/tutorial/crud`

- :doc:`/tutorial/commands`
Expand Down
23 changes: 20 additions & 3 deletions docs/reference/method/MongoDBClient__construct.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,27 @@ initialized on demand, when the first operation is executed.
Examples
--------

.. start-connecting-include

Connecting to a Standalone server
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

If you do not specify a ``$uri`` value, the driver connects to a standalone
:program:`mongod` on ``127.0.0.1`` via port ``27017``. To connect to a different
server, pass the corresponding connection string as the first parameter when
creating the :phpclass:`Client <MongoDB\\Client>` instance:

.. code-block:: php

<?php

$client = new MongoDB\Client('mongodb://mongodb-deployment:27017');

Connecting to a Replica Set
~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

.. code-block:: php

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

.. end-connecting-include

Specifying a Custom Type Map
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
1 change: 1 addition & 0 deletions docs/tutorial.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Tutorials

.. toctree::

/tutorial/connecting
/tutorial/crud
/tutorial/collation
/tutorial/commands
Expand Down
25 changes: 25 additions & 0 deletions docs/tutorial/connecting.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
=====================
Connecting to MongoDB
=====================

.. default-domain:: mongodb

.. contents:: On this page
:local:
:backlinks: none
:depth: 2
:class: singlecol

Creating a Client instance
--------------------------------------------------------

.. include:: /reference/method/MongoDBClient__construct.txt
:start-after: start-connecting-include
:end-before: end-connecting-include

Specifying connection options
-----------------------------

Connection options can be passed via the ``$uri`` parameter, or through the
``$options`` and ``$driverOptions`` parameters. The available options are
documented in the :phpmethod:`MongoDB\\Client::__construct()` reference.