MongoCollection::ensureIndex
(PECL mongo >=0.9.0)
MongoCollection::ensureIndex — Creates an index on the given field(s), or does nothing if the index already exists
Description
public boolean MongoCollection::ensureIndex
( string|array $keys
, boolean $unique
)
A unique index cannot be created on a field if multiple existing documents do not contain the field. The field is effectively NULL for these documents and thus already non-unique.
Parameters
- keys
-
Field or fields to use as index.
- unique
-
If the index should be unique.
Return Values
Returns TRUE.
Examples
Example #1 MongoCollection::ensureIndex() example
<?php
$c = new MongoCollection($db, 'foo');
// create an index on 'x' ascending
$c->ensureIndex('x');
// create an index on 'y' ascending
$c->ensureIndex(array('y' => 1));
// create an index on 'w' descending
$c->ensureIndex(array('w' => -1));
// create an index on 'z' ascending and 'zz' descending
$c->ensureIndex(array('z' => 1, 'zz' => -1));
// create a unique index on 'x'
$c->ensureIndex(array('x' => 1), true);
?>
MongoCollection::ensureIndex
There are no user contributed notes for this page.
