©
本文档使用
php.cn手册 发布
After the initial set-up, we continue explaining how to get started with the MongoDB driver and library for HHVM to write our first project.
The last thing we still need to install to get started on the application itself, is the PHP library.
The library needs to be installed with Composer. In your project directory
(/var/www/html/my-first-project
) type:
curl -sS https://getcomposer.org/installer -o installer.php hhvm installer.php rm installer.php
This downloads and installs Composer. Wherever it says "Use it: php composer.phar", it of course means hhvm composer.phar.
With Composer installed, we can now install the library:
hhvm composer.phar require mongodb/mongodb
It outputs something akin to:
Using version ^0.2.0 for mongodb/mongodb ./composer.json has been created Loading composer repositories with package information Updating dependencies (including require-dev) - Installing mongodb/mongodb (0.2.0) Downloading: 100% Writing lock file Generating autoload files
And it has created several files (composer.json
,
composer.lock
) as well as the vendor
directory
that contains the library.
Composer manages your dependencies, and will provide you with a loader that you include with the following at the start of your script:
<?php
require 'vendor/autoload.php' ;
With this done, you can now use any of the functionality as described in the » documentation.
If you are familiar with the old driver, it should look too much out of place. The only big difference is that the » Database class is only used for Database specific operations. The » CRUD operations on the » Collection class are also renamed for clarity, and to be in accordance with a new language-agnostic » specification.
As an example, this is how you insert a document into the beers collection of the demo database:
<?php
require 'vendor/autoload.php' ; // include Composer goodies
$manager = new MongoDB \ Driver \ Manager ( "mongodb://localhost:27017" );
$collection = new MongoDB \ Collection ( $manager , "demo.beers" );
$result = $collection -> insertOne ( [ 'name' => 'Hinterland' , 'brewery' => 'BrewDog' ] );
echo "Inserted with Object ID ' { $result -> getInsertedId ()} '" ;
?>
Instead of the original document being modified to add the newly generated
_id
field, this is now part of the result that comes back
from the insertOne
method.
After insertion, you can of course also query the data that you have just
inserted. For that, you use the find
method which returns a
cursor that you can iterate over:
<?php
require 'vendor/autoload.php' ; // include Composer goodies
$manager = new MongoDB \ Driver \ Manager ( "mongodb://localhost:27017" );
$collection = new MongoDB \ Collection ( $manager , "demo.beers" );
$result = $collection -> find ( [ 'name' => 'Hinterland' , 'brewery' => 'BrewDog' ] );
foreach ( $result as $entry )
{
echo $entry -> _id , ': ' , $entry -> name , "\n" ;
}
?>
You might have noticed that instead of accessing the _id
and
name
fields is no longer done through an array access
operator. Instead, they are now properties of a stdClass
object. You can find more information on how serialisation and
deserialisation between PHP variables and the BSON stored in MongoDB in
the Persisting Data specification.
[#1] andres dot rosado at upr dot edu [2015-11-20 19:36:26]
The documentation hasn't kept up with the updates to the libraries.
To install the Composer library, make sure to install the mongodb.so installed and configured. Then, you need to run the following command:
./composer.phar require "mongodb/mongodb=^1.0.0@beta"
The current stable version (0.2.0 as of this writing), doesn't work well with the current MongoDB extension, but the 1.0.0 beta version works fine.