Sokil\Mongo\Collection::ensureTTLIndex PHP Method

ensureTTLIndex() public method

Create TTL index
public ensureTTLIndex ( array $key, integer $seconds ) : Collection
$key array key must be date to use TTL
$seconds integer
return Collection
    public function ensureTTLIndex(array $key, $seconds = 0)
    {
        $this->getMongoCollection()->createIndex($key, array('expireAfterSeconds' => $seconds));
        return $this;
    }

Usage Example

Example #1
0
 public function testEnsureTTLIndex()
 {
     $this->collection->ensureTTLIndex(array('ttlAsc' => 1, 'ttlDesc' => -1), 12000);
     $indexes = $this->collection->getIndexes();
     $this->assertEquals(array('ttlAsc' => 1, 'ttlDesc' => -1), $indexes[1]['key']);
     $this->assertArrayHasKey('expireAfterSeconds', $indexes[1]);
     $this->assertEquals(12000, $indexes[1]['expireAfterSeconds']);
 }