MongoDB::createCollection
(PECL mongo >=0.9.0)
MongoDB::createCollection — Creates a collection
Description
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,
"capped" => $options["capped"],
"size" => $options["size"],
"max" => $options["max"],
"autoIndexId" => $options["autoIndexId"],
));
?>
Parameters
-
name
-
The name of the collection.
-
options
-
An array containing options for the collections. Each option is its own element in the options array, with the option name listed below being the key of the element. The supported options depend on the MongoDB server version. At the moment, the following options are supported:
-
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.
-
autoIndexId
-
If capped is
TRUE
you can specifyFALSE
to disable the automatic index created on the _id field. Before MongoDB 2.2, the default value for autoIndexId wasFALSE
.
-
Return Values
Returns a collection object representing the new collection.
Examples
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",
array(
'capped' => true,
'size' => 10*1024,
'max' => 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:
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
Changelog
Version | Description |
---|---|
1.4.0 |
In versions before 1.4.0, the options were all arguments to the method. The function synopsis in those older versions is:
public MongoCollection MongoDB::createCollection
( string
$name
[, bool $capped = FALSE
[, int $size = 0
[, int $max = 0
]]] )
The meaning of the options is the same as described under the
|
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Расширения для работы с базами данных
- Расширения для работы с базами данных отдельных производителей
- MongoDB
- Базовые классы
- Функция MongoDB::authenticate() - Log in to this database
- Функция MongoDB::command() - Execute a database command
- Функция MongoDB::__construct() - Creates a new database
- Функция MongoDB::createCollection() - Creates a collection
- Функция MongoDB::createDBRef() - Creates a database reference
- Функция MongoDB::drop() - Drops this database
- Функция MongoDB::dropCollection() - Drops a collection [deprecated]
- Функция MongoDB::execute() - Runs JavaScript code on the database server.
- Функция MongoDB::forceError() - Creates a database error
- Функция MongoDB::__get() - Gets a collection
- MongoDB::getCollectionInfo
- Функция MongoDB::getCollectionNames() - Get all collections from this database
- Функция MongoDB::getDBRef() - Fetches the document pointed to by a database reference
- Функция MongoDB::getGridFS() - Fetches toolkit for dealing with files stored in this database
- Функция MongoDB::getProfilingLevel() - Gets this database's profiling level
- Функция MongoDB::getReadPreference() - Get the read preference for this database
- Функция MongoDB::getSlaveOkay() - Get slaveOkay setting for this database
- Функция MongoDB::getWriteConcern() - Get the write concern for this database
- Функция MongoDB::lastError() - Check if there was an error on the most recent db operation performed
- Функция MongoDB::listCollections() - Gets an array of all MongoCollections for this database
- Функция MongoDB::prevError() - Checks for the last error thrown during a database operation
- Функция MongoDB::repair() - Repairs and compacts this database
- Функция MongoDB::resetError() - Clears any flagged errors on the database
- Функция MongoDB::selectCollection() - Gets a collection
- Функция MongoDB::setProfilingLevel() - Sets this database's profiling level
- Функция MongoDB::setReadPreference() - Set the read preference for this database
- Функция MongoDB::setSlaveOkay() - Change slaveOkay setting for this database
- Функция MongoDB::setWriteConcern() - Set the write concern for this database
- Функция MongoDB::__toString() - The name of this database
Коментарии
404 Not Found