File: library.xml

package info (click to toggle)
php-doc 20250827~git.abe740d%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 71,968 kB
  • sloc: xml: 985,760; php: 25,504; javascript: 671; sh: 177; makefile: 37
file content (178 lines) | stat: -rw-r--r-- 5,717 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?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
-->