<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Using the PHP Library for MongoDB (PHPLIB)</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="mongodb.tutorial.html">Tutorials</a></div> <div class="next" style="text-align: right; float: right;"><a href="mongodb.tutorial.apm.html">Application Performance Monitoring (APM)</a></div> <div class="up"><a href="mongodb.tutorial.html">Tutorials</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="mongodb.tutorial.library" class="chapter"> <h1>Using the PHP Library for MongoDB (PHPLIB)</h1> <p class="para"> After the initial driver set-up, we will continue explaining how to get started with the MongoDB driver and corresponding userland library to write our first project. </p> <div class="section"> <h2 class="title">Installing the PHP Library with Composer</h2> <p class="para"> The last thing we still need to install to get started on the application itself, is the PHP library. </p> <p class="para"> The library needs to be installed with <a href="https://getcomposer.org/" class="link external">» Composer</a>, a package manager for PHP. Instructions for installing Composer on various platforms may be found on its website. </p> <p class="para"> Install the library by running: <div class="example-contents"> <div class="shellcode"><pre class="shellcode">$ composer require mongodb/mongodb</pre> </div> </div> </p> <p class="para"> It will output something akin to: <div class="example-contents"> <div class="textcode"><pre class="textcode">./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</pre> </div> </div> </p> <p class="para"> Composer will create several files: <code class="code">composer.json</code>, <code class="code">composer.lock</code>, and a <code class="code">vendor</code> directory that will contain the library and any other dependencies your project might require. </p> </div> <div class="section"> <h2 class="title">Using the PHP Library</h2> <p class="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: <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// This path should point to Composer's autoloader<br /></span><span style="color: #007700">require </span><span style="color: #DD0000">'vendor/autoload.php'</span><span style="color: #007700">;</span> </span> </code></div> </div> </p> <p class="para"> With this done, you can now use any of the functionality as described in the <a href="https://docs.mongodb.com/php-library" class="link external">» library documentation</a>. </p> <p class="para"> If you have previously used the old driver (i.e. <code class="code">mongo</code> extension), the library's API should look familiar. It contains a <a href="https://docs.mongodb.com/php-library/master/reference/class/MongoDBClient/" class="link external">» Client</a> class for connecting to MongoDB, and <a href="https://docs.mongodb.com/php-library/master/reference/class/MongoDBDatabase/" class="link external">» Database</a> class for database-level operations (e.g. commands, collection management) and a <a href="https://docs.mongodb.com/php-library/master/reference/class/MongoDBCollection" class="link external">» Collection</a> class for collection-level operations (e.g. <a href="https://en.wikipedia.org/wiki/Create,_read,_update_and_delete" class="link external">» CRUD</a> methods, index management). Various Collection methods have been renamed for clarity, and to be in accordance with a new language-agnostic <a href="https://github.com/mongodb/specifications/blob/master/source/crud/crud.rst" class="link external">» specification</a>. </p> <p class="para"> As an example, this is how you insert a document into the <em class="emphasis">beers</em> collection of the <em class="emphasis">demo</em> database: <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">require </span><span style="color: #DD0000">'vendor/autoload.php'</span><span style="color: #007700">; </span><span style="color: #FF8000">// include Composer's autoloader<br /><br /></span><span style="color: #0000BB">$client </span><span style="color: #007700">= new </span><span style="color: #0000BB">MongoDB</span><span style="color: #007700">\</span><span style="color: #0000BB">Client</span><span style="color: #007700">(</span><span style="color: #DD0000">"mongodb://localhost:27017"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$collection </span><span style="color: #007700">= </span><span style="color: #0000BB">$client</span><span style="color: #007700">-></span><span style="color: #0000BB">demo</span><span style="color: #007700">-></span><span style="color: #0000BB">beers</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">$collection</span><span style="color: #007700">-></span><span style="color: #0000BB">insertOne</span><span style="color: #007700">( [ </span><span style="color: #DD0000">'name' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'Hinterland'</span><span style="color: #007700">, </span><span style="color: #DD0000">'brewery' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'BrewDog' </span><span style="color: #007700">] );<br /><br />echo </span><span style="color: #DD0000">"Inserted with Object ID '</span><span style="color: #007700">{</span><span style="color: #0000BB">$result</span><span style="color: #007700">-></span><span style="color: #0000BB">getInsertedId</span><span style="color: #007700">()}</span><span style="color: #DD0000">'"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </p> <p class="para"> Instead of injecting the generated <code class="code">_id</code> field into the input document (as was done in the old driver), it is now made available through the result object returned by the <code class="code">insertOne</code> method. </p> <p class="para"> After insertion, you can of course also query the data that you have just inserted. For that, you use the <code class="code">find</code> method, which returns an iterable cursor: <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">require </span><span style="color: #DD0000">'vendor/autoload.php'</span><span style="color: #007700">; </span><span style="color: #FF8000">// include Composer's autoloader<br /><br /></span><span style="color: #0000BB">$client </span><span style="color: #007700">= new </span><span style="color: #0000BB">MongoDB</span><span style="color: #007700">\</span><span style="color: #0000BB">Client</span><span style="color: #007700">(</span><span style="color: #DD0000">"mongodb://localhost:27017"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$collection </span><span style="color: #007700">= </span><span style="color: #0000BB">$client</span><span style="color: #007700">-></span><span style="color: #0000BB">demo</span><span style="color: #007700">-></span><span style="color: #0000BB">beers</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">$collection</span><span style="color: #007700">-></span><span style="color: #0000BB">find</span><span style="color: #007700">( [ </span><span style="color: #DD0000">'name' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'Hinterland'</span><span style="color: #007700">, </span><span style="color: #DD0000">'brewery' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'BrewDog' </span><span style="color: #007700">] );<br /><br />foreach (</span><span style="color: #0000BB">$result </span><span style="color: #007700">as </span><span style="color: #0000BB">$entry</span><span style="color: #007700">) {<br /> echo </span><span style="color: #0000BB">$entry</span><span style="color: #007700">[</span><span style="color: #DD0000">'_id'</span><span style="color: #007700">], </span><span style="color: #DD0000">': '</span><span style="color: #007700">, </span><span style="color: #0000BB">$entry</span><span style="color: #007700">[</span><span style="color: #DD0000">'name'</span><span style="color: #007700">], </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </p> <p class="para"> While it may not be apparent in the examples, BSON documents and arrays are unserialized as type classes in the library by default. These classes ensure that values preserve their type when being serialized back into BSON, which avoids a caveat in the old driver where arrays might turn into documents, and vice versa. Additionally, the classes extend <a href="class.arrayobject.html" class="classname">ArrayObject</a> for enhanced usability. You can find more information on how serialization and deserialization between PHP variables and BSON is handled by the driver and library by reading the <a href="mongodb.persistence.html" class="xref">Persisting Data</a> specification. </p> </div> </div> <hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="mongodb.tutorial.html">Tutorials</a></div> <div class="next" style="text-align: right; float: right;"><a href="mongodb.tutorial.apm.html">Application Performance Monitoring (APM)</a></div> <div class="up"><a href="mongodb.tutorial.html">Tutorials</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>