PHP Portal » PHP Handbuch » MongoDB::createCollection

Werbung

MongoDB::createCollection


(PECL mongo >=0.9.0)

MongoDB::createCollectionCreates a collection

Beschreibung

public MongoCollection MongoDB::createCollection ( string $name [, bool $capped = FALSE [, int $size = 0 [, int $max = 0 ]]] )

Parameter-Liste

name

The name of the collection.

capped

If the collection should be a fixed size.

size

If the collection is fixed size, its size in bytes.

max

If the collection is fixed size, the maximum number of elements to store in the collection.

Rückgabewerte

Returns a collection object representing the new collection.

Beispiele

Beispiel #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 Code
1
2
3
4
5
6
7
8
$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"; }

Das oben gezeigte Beispiel erzeugt eine ähnliche Ausgabe wie:


sample log message #90
sample log message #91
sample log message #92
sample log message #93
sample log message #94
sample log message #95
sample log message #96
sample log message #97
sample log message #98
sample log message #99