Using MongoDB with PHP: Using Collections

In subsequent subsections we shall discuss getting a collection and dropping a collection using the PHP MongoDB Driver.

1. Getting a Collection

In this section we shall create a collection in a MongoDB database instance. Create a PHP script collection.php in the C:\php directory. The MongoCollection class represents a collection. The syntax to get a collection from a connection instance is the same as for getting a database. For example first get the database instance local and subsequently get the collection catalog from the database instance as follows.

$connection = new MongoClient();

$db=$connection->local;

$collection=$db->catalog;

The collection info and collection name may be output as follows.

var_dump($collection);

var_dump($collection->getName());

A collection may also be gotten directly as follows.

$collection=$connection->local->mongo;

The MongoDB::createCollection() may also be used to create a MongoCollection instance. The createCollection() method has the following syntax.

$db->createCollection(

“create” => $name,

“capped” => $options[“capped”],

“size” => $options[“size”],

“max” => $options[“max”],

“autoIndexId” => $options[“autoIndexId”],

));

The parameters and options in the createCollection() method are as follows in Table 3-4.

For example, create a capped collection called catalog with fixed size of 1 MB, and maximum number of documents as 10.

$coll = $db->createCollection( “catalog”, array(

‘capped’ => true,

‘size’ => 1*1024,

‘max’ => 10

)

);

The collection.php script is listed below.

<?php

try

{

$connection = new MongoClient();

$db=$connection->local;

$collection=$db->catalog;

var_dump($collection);

print ‘<br/>’;

print ‘Collection Name: ‘;

var_dump($collection->getName());

print ‘<br/>’;

$collection=$connection->local->mongo;

print ‘Collection Name: ‘;

var_dump($collection->getName());

}catch ( MongoConnectionException $e )

{

echo ‘<p>Couldn\’t connect to mongodb, is the “mongo” process running?</p>’;

exit();

}

$collection->drop();

$coll = $db->createCollection(

“catalog”,

array(

‘capped’ => true,

‘size’ => 1*1024,

‘max’ => 10

)

);

print ‘<br/>’;

var_dump($coll);

print ‘<br/>’;

print ‘Collection Name: ‘;

var_dump($coll->getName());

?>

Run the collection.php script in a browser with the URL http://localhost:8000/collection.php. The output is shown in Figure 3-8.

2. Dropping a Collection

The MongoCollection::drop() method drops a collection and does not have any parameters.

Create a PHP script dropCollection.php in the C:\php directory. In a try-catch statement create a MongoCollection instance. Invoke the drop() method to drop the collection.

$collection->drop();

The dropCollection.php script is listed:

<?php

try

{

$connection = new MongoClient();

$collection=$connection->local->catalog;

$collection->drop();

echo ‘<p>Collection Dropped</p>’;

}catch (MongoConnectionException $e)

{

echo ‘<p>Couldn\’t connect to mongodb</p>’;

exit();

}

?>

Run the PHP script in the browser with URL http://localhost:8000/dropCollection.php as shown in Figure 3-9.

The catalog collection gets dropped as indicated by the show collections method run before and after running the dropCollection.php script in Figure 3-10.

Source: Vohra Deepak (2015), Pro MongoDB™ Development, Apress; 1st ed. edition.

Leave a Reply

Your email address will not be published. Required fields are marked *