(PECL mongo >=0.9.0)
MongoDB::createCollection — Creates a collection
This method is used to create capped collections and other collections requiring special options. It is identical to running:
<?php
$collection = $db->command(array("create" => $name, "size" => $size, "capped" => $capped, "max" => $max));
?>
The name of the collection.
If the collection should be a fixed size.
If the collection is fixed size, its size in bytes.
If the collection is fixed size, the maximum number of elements to store in the collection.
Returns a collection object representing the new collection.
Example #1 MongoDB::createCollection() capped collection example
A capped collection is a special type of collection that has either a fixed or a fixed number of elements. Once the collection is "full," the oldest elements will be removed when new elements are added. Capped collections can be very useful for applications like logging, where you may want to reserve a certain amount of space for logs and not worry about them getting too big.
This example creates a very tiny log collection that will keep a maximum of 10 documents.
<?php
$log = $db->createCollection("logger", true, 10*1024, 10);
for ($i = 0; $i < 100; $i++) {
$log->insert(array("level" => WARN, "msg" => "sample log message #$i", "ts" => new MongoDate()));
}
$msgs = $log->find();
foreach ($msgs as $msg) {
echo $msg['msg']."\n";
}
?>
The above example will output something similar to: