Sokil\Mongo\Collection::ensure2dIndex PHP Method

ensure2dIndex() public method

Create geo index 2dsphere
public ensure2dIndex ( string $field ) : Collection
$field string
return Collection
    public function ensure2dIndex($field)
    {
        if (is_array($field)) {
            $keys = array_fill_keys($field, '2d');
        } else {
            $keys = array($field => '2d');
        }
        $this->getMongoCollection()->createIndex($keys);
        return $this;
    }

Usage Example

Ejemplo n.º 1
0
 public function testExpressionWithinPolygon()
 {
     $this->collection->ensure2dIndex('point');
     $point1Id = $this->collection->createDocument()->setLegacyPoint('point', 5, 4)->save()->getId();
     $point2Id = $this->collection->createDocument()->setLegacyPoint('point', 50, 40)->save()->getId();
     $point = $this->collection->find()->withinPolygon('point', array(array(0, 0), array(0, 10), array(10, 10), array(10, 0)))->findOne();
     $this->assertNotEmpty($point);
     $this->assertEquals($point1Id, $point->getId());
 }