1
0
mirror of https://github.com/php/doc-en.git synced 2026-03-24 07:42:10 +01:00
Files
haszi 3e871fe7ea Convert MongoDB documentation from a top element of set to book (GH-3627)
* Convert MongoDB documentation from a top element of set to book

Convert all underlying elements as needed:
 - books to parts or chapters
 - articles to sections

Move the Predefined Constants page from the Installing/Configuring section to its own

Change the following xml:ids:
 - book.mongodb to mongodb.mongodb
 - set.mongodb to book.mongodb
 - book.bson to mongodb.bson

* [skip-revcheck] Remove trailing whitespace

* Add extension membership

---------

Co-authored-by: haszi <haszika80@gmail.com>
2024-10-21 13:11:18 +02:00

179 lines
5.6 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<section xml:id="mongodb.tutorial.library" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<title>Using the PHP Library for MongoDB (PHPLIB)</title>
<para>
After the initial extension set-up, we will continue explaining how to get
started with the corresponding userland library to write our first project.
</para>
<section>
<title>Installing the PHP Library with Composer</title>
<para>
The last thing we still need to install to get started on the application
itself, is the PHP library.
</para>
<para>
The library needs to be installed with
<link xlink:href="&url.mongodb.composer;">Composer</link>, a package manager
for PHP. Instructions for installing Composer on various platforms may be
found on its website.
</para>
<para>
Install the library by running:
<programlisting role="shell">
<![CDATA[
$ composer require mongodb/mongodb
]]>
</programlisting>
</para>
<para>
It will output something akin to:
<programlisting role="text">
<![CDATA[
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
- Installing mongodb/mongodb (1.0.0)
Downloading: 100%
Writing lock file
Generating autoload files
]]>
</programlisting>
</para>
<para>
Composer will create several files: <code>composer.json</code>,
<code>composer.lock</code>, and a <code>vendor</code> directory that will
contain the library and any other dependencies your project might require.
</para>
</section>
<section>
<title>Using the PHP Library</title>
<para>
In addition to managing your dependencies, Composer will also provide you
with an autoloader (for those dependencies' classes). Ensure that it is
included at the start of your script or in your application's bootstrap
code:
<programlisting role="php">
<![CDATA[
<?php
// This path should point to Composer's autoloader
require 'vendor/autoload.php';
]]>
</programlisting>
</para>
<para>
With this done, you can now use any of the functionality as described in the
<link xlink:href="&url.mongodb.library.docs;">library documentation</link>.
</para>
<para>
If you have used MongoDB drivers in other languages, the library's API
should look familiar. It contains a
<link xlink:href="&url.mongodb.library.apidocs;/class/MongoDBClient/">Client</link>
class for connecting to MongoDB, a
<link xlink:href="&url.mongodb.library.apidocs;/class/MongoDBDatabase/">Database</link>
class for database-level operations (e.g. commands, collection management),
and a
<link xlink:href="&url.mongodb.library.apidocs;/class/MongoDBCollection">Collection</link>
class for collection-level operations (e.g.
<link xlink:href="&url.mongodb.wiki.crud;">CRUD</link> methods, index management).
</para>
<para>
As an example, this is how you insert a document into the
<emphasis>beers</emphasis> collection of the <emphasis>demo</emphasis>
database:
<programlisting role="php">
<![CDATA[
<?php
require 'vendor/autoload.php'; // include Composer's autoloader
$client = new MongoDB\Client("mongodb://localhost:27017");
$collection = $client->demo->beers;
$result = $collection->insertOne( [ 'name' => 'Hinterland', 'brewery' => 'BrewDog' ] );
echo "Inserted with Object ID '{$result->getInsertedId()}'";
?>
]]>
</programlisting>
</para>
<para>
Since the inserted document did not contain an <code>_id</code> field, the
extension will generate an <classname>MongoDB\BSON\ObjectId</classname> for
the server to use as the <code>_id</code>. This value is also made available
to the caller via the result object returned by the <code>insertOne</code>
method.
</para>
<para>
After insertion, you can query for the data that you have just inserted.
For that, you use the <code>find</code> method, which returns an iterable
cursor:
<programlisting role="php">
<![CDATA[
<?php
require 'vendor/autoload.php'; // include Composer's autoloader
$client = new MongoDB\Client("mongodb://localhost:27017");
$collection = $client->demo->beers;
$result = $collection->find( [ 'name' => 'Hinterland', 'brewery' => 'BrewDog' ] );
foreach ($result as $entry) {
echo $entry['_id'], ': ', $entry['name'], "\n";
}
?>
]]>
</programlisting>
</para>
<para>
While it may not be apparent in the examples, BSON documents and arrays are
unserialized as special classes in the library by default. These classes
extend <classname>ArrayObject</classname> for usability and implement the
extension's <interfacename>MongoDB\BSON\Serializable</interfacename> and
<interfacename>MongoDB\BSON\Unserializable</interfacename> interfaces to
ensure that values preserve their type when serialized back into BSON. This
avoids a caveat in the legacy <code>mongo</code> extension where arrays
might turn into documents, and vice versa. See the
<xref linkend="mongodb.persistence"/> specification for more information on
how values are converted between PHP and BSON.
</para>
</section>
</section>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->