Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/reference/enumeration-classes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,10 @@ Methods
/reference/method/MongoDBModelIndexInfo-getName
/reference/method/MongoDBModelIndexInfo-getNamespace
/reference/method/MongoDBModelIndexInfo-getVersion
/reference/method/MongoDBModelIndexInfo-is2dSphere
/reference/method/MongoDBModelIndexInfo-isGeoHaystack
/reference/method/MongoDBModelIndexInfo-isSparse
/reference/method/MongoDBModelIndexInfo-isText
/reference/method/MongoDBModelIndexInfo-isTtl
/reference/method/MongoDBModelIndexInfo-isUnique

Expand Down
59 changes: 59 additions & 0 deletions docs/reference/method/MongoDBModelIndexInfo-is2dSphere.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
=======================================
MongoDB\\Model\\IndexInfo::is2dSphere()
=======================================

.. versionadded:: 1.4

.. default-domain:: mongodb

.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol

Definition
----------

.. phpmethod:: MongoDB\\Model\\IndexInfo::is2dSphere()

Return whether the index is a :manual:`2dsphere </core/2dsphere>`
index.

.. code-block:: php

function is2dSphere(): boolean

Return Values
-------------

A boolean indicating whether the index is a 2dsphere index.

Examples
--------

.. code-block:: php

<?php

$collection = (new MongoDB\Client)->selectCollection('test', 'places');

$collection->createIndex(['pos' => '2dsphere']);

foreach ($collection->listIndexes() as $index) {
if ($index->is2dSphere()) {
printf("%s has 2dsphereIndexVersion: %d\n", $index->getName(), $index['2dsphereIndexVersion']);
}
}

The output would then resemble::

pos_2dsphere has 2dsphereIndexVersion: 3

See Also
--------

- :phpmethod:`MongoDB\\Collection::createIndex()`
- :phpmethod:`MongoDB\\Collection::listIndexes()`
- :manual:`2dsphere Indexes </core/2dsphere>` reference in the MongoDB
manual
59 changes: 59 additions & 0 deletions docs/reference/method/MongoDBModelIndexInfo-isGeoHaystack.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
==========================================
MongoDB\\Model\\IndexInfo::isGeoHaystack()
==========================================

.. versionadded:: 1.4

.. default-domain:: mongodb

.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol

Definition
----------

.. phpmethod:: MongoDB\\Model\\IndexInfo::isGeoHaystack()

Return whether the index is a :manual:`geoHaystack
</core/geohaystack>` index.

.. code-block:: php

function isGeoHaystack(): boolean

Return Values
-------------

A boolean indicating whether the index is a geoHaystack index.

Examples
--------

.. code-block:: php

<?php

$collection = (new MongoDB\Client)->selectCollection('test', 'places');

$collection->createIndex(['pos' => 'geoHaystack', 'x' => 1], ['bucketSize' => 5]);

foreach ($collection->listIndexes() as $index) {
if ($index->isGeoHaystack()) {
printf("%s has bucketSize: %d\n", $index->getName(), $index['bucketSize']);
}
}

The output would then resemble::

pos_geoHaystack_x_1 has bucketSize: 5

See Also
--------

- :phpmethod:`MongoDB\\Collection::createIndex()`
- :phpmethod:`MongoDB\\Collection::listIndexes()`
- :manual:`geoHaystack Indexes </core/geohaystack>` reference in the MongoDB
manual
58 changes: 58 additions & 0 deletions docs/reference/method/MongoDBModelIndexInfo-isText.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
===================================
MongoDB\\Model\\IndexInfo::isText()
===================================

.. versionadded:: 1.4

.. default-domain:: mongodb

.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol

Definition
----------

.. phpmethod:: MongoDB\\Model\\IndexInfo::isText()

Return whether the index is a :manual:`text </core/index-text>` index.

.. code-block:: php

function isText(): boolean

Return Values
-------------

A boolean indicating whether the index is a text index.

Examples
--------

.. code-block:: php

<?php
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Newline after.


$collection = (new MongoDB\Client)->selectCollection('test', 'restaurants');

$collection->createIndex(['name' => 'text']);

foreach ($collection->listIndexes() as $index) {
if ($index->isText()) {
printf("%s has default language: %d\n", $index->getName(), $index['default_language']);
}
}

The output would then resemble::

name_text has default language: english

See Also
--------

- :phpmethod:`MongoDB\\Collection::createIndex()`
- :phpmethod:`MongoDB\\Collection::listIndexes()`
- :manual:`Text Indexes </core/index-text>` reference in the MongoDB
manual
30 changes: 30 additions & 0 deletions src/Model/IndexInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,26 @@ public function getVersion()
return (integer) $this->info['v'];
}

/**
* Return whether or not this index is of type 2dsphere.
*
* @return boolean
*/
public function is2dSphere()
{
return array_search('2dsphere', $this->getKey(), true) !== false;
}

/**
* Return whether or not this index is of type geoHaystack.
*
* @return boolean
*/
public function isGeoHaystack()
{
return array_search('geoHaystack', $this->getKey(), true) !== false;
}

/**
* Return whether this is a sparse index.
*
Expand All @@ -121,6 +141,16 @@ public function isSparse()
return ! empty($this->info['sparse']);
}

/**
* Return whether or not this index is of type text.
*
* @return boolean
*/
public function isText()
{
return array_search('text', $this->getKey(), true) !== false;
}

/**
* Return whether this is a TTL index.
*
Expand Down
78 changes: 78 additions & 0 deletions tests/Model/IndexInfoFunctionalTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace MongoDB\Tests\Model;

use MongoDB\Collection;
use MongoDB\Tests\FunctionalTestCase;

class IndexInfoFunctionalTest extends FunctionalTestCase
{
private $collection;

public function setUp()
{
parent::setUp();

$this->collection = new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName());
Copy link
Member

@jmikola jmikola Apr 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're creating a collection in each test, let's drop it upon setup() and tearDown(). Consider MongoDB\Tests\Collection\FunctionalTestCase for prior art. In particular, we can leave the collection as-is in the event of a failure.

I see no reason we can't use the Collection methods, so $this->collection->drop(); is fine and we can skip the DropCollection operation class.

$this->collection->drop();
}

public function tearDown()
{
if ($this->hasFailed()) {
return;
}

$this->collection->drop();
}

public function testIs2dSphere()
{
$indexName = $this->collection->createIndex(['pos' => '2dsphere']);
$result = $this->collection->listIndexes();

$result->rewind();
$result->next();
$index = $result->current();

$this->assertEquals($indexName, $index->getName());
$this->assertTrue($index->is2dSphere());

$expectedVersion = version_compare($this->getServerVersion(), '3.2.0', '<') ? 2 : 3;
$this->assertEquals($expectedVersion, $index['2dsphereIndexVersion']);
}

public function testIsGeoHaystack()
{
$indexName = $this->collection->createIndex(['pos' => 'geoHaystack', 'x' => 1], ['bucketSize' => 5]);
$result = $this->collection->listIndexes();

$result->rewind();
$result->next();
$index = $result->current();

$this->assertEquals($indexName, $index->getName());
$this->assertTrue($index->isGeoHaystack());
$this->assertEquals(5, $index['bucketSize']);
}

public function testIsText()
{
$indexName = $this->collection->createIndex(['x' => 'text']);
$result = $this->collection->listIndexes();

$result->rewind();
$result->next();
$index = $result->current();

$this->assertEquals($indexName, $index->getName());
$this->assertTrue($index->isText());
$this->assertEquals('english', $index['default_language']);
$this->assertEquals('language', $index['language_override']);

$expectedVersion = version_compare($this->getServerVersion(), '3.2.0', '<') ? 2 : 3;
$this->assertEquals($expectedVersion, $index['textIndexVersion']);

$this->assertSameDocument(['x' => 1], $index['weights']);
}
}
Loading