be careful when using ensureIndex('loc'=>'2d')
for example, if the data structure of loc is array('lng'=>120, 'lat'=>-30), when it is saved into database, its structure will be ['lat':-30, 'lng':120], it is because the array in PHP is the ordered map, not the order of literal in your code. so the 2d-index is mapped as (lat, lng). when you find your location with (lng, lat), the wrong results are returned.
be careful
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
Описание
public boolean MongoCollection::ensureIndex
( string|array $keys
)
Параметри
- keys
-
Field or fields to use as index.
Връщани стойности
Returns TRUE.
Примери
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));
?>
coffee dot stone at gmail dot com
17-Apr-2012 08:04
alan dot lake at lakeinfoworks dot com
26-Aug-2010 08:45
The statement
<?php
$c->ensureIndex(array('x' => 1), array("unique" => true));
?>
will prevent a document with a duplicate key ('x') from being added to the connection, but will not throw an exception. To cause an exception to be thrown, use the "safe"=>true option in the insert statement.
