Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4b54760

Browse files
committed
PHPLIB-80: Add IndexInfo helper methods for special index type options
1 parent 80dd783 commit 4b54760

File tree

7 files changed

+362
-0
lines changed

7 files changed

+362
-0
lines changed

‎docs/reference/enumeration-classes.txt‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,10 @@ Methods
134134
/reference/method/MongoDBModelIndexInfo-getName
135135
/reference/method/MongoDBModelIndexInfo-getNamespace
136136
/reference/method/MongoDBModelIndexInfo-getVersion
137+
/reference/method/MongoDBModelIndexInfo-is2dSphere
138+
/reference/method/MongoDBModelIndexInfo-isGeoHaystack
137139
/reference/method/MongoDBModelIndexInfo-isSparse
140+
/reference/method/MongoDBModelIndexInfo-isText
138141
/reference/method/MongoDBModelIndexInfo-isTtl
139142
/reference/method/MongoDBModelIndexInfo-isUnique
140143

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
=======================================
2+
MongoDB\\Model\\IndexInfo::is2dSphere()
3+
=======================================
4+
5+
.. versionadded:: 1.4
6+
7+
.. default-domain:: mongodb
8+
9+
.. contents:: On this page
10+
:local:
11+
:backlinks: none
12+
:depth: 1
13+
:class: singlecol
14+
15+
Definition
16+
----------
17+
18+
.. phpmethod:: MongoDB\\Model\\IndexInfo::is2dSphere()
19+
20+
Return whether the index is a :manual:`2dsphere </core/2dsphere>`
21+
index.
22+
23+
.. code-block:: php
24+
25+
function is2dSphere(): boolean
26+
27+
Return Values
28+
-------------
29+
30+
A boolean indicating whether the index is a 2dsphere index.
31+
32+
Examples
33+
--------
34+
35+
.. code-block:: php
36+
37+
<?php
38+
39+
$collection = (new MongoDB\Client)->selectCollection('test', 'places');
40+
41+
$collection->createIndex(['pos' => '2dsphere']);
42+
43+
foreach ($collection->listIndexes() as $index) {
44+
if ($index->is2dSphere()) {
45+
printf("%s has 2dsphereIndexVersion: %d\n", $index->getName(), $index['2dsphereIndexVersion']);
46+
}
47+
}
48+
49+
The output would then resemble::
50+
51+
pos_2dsphere has 2dsphereIndexVersion: 3
52+
53+
See Also
54+
--------
55+
56+
- :phpmethod:`MongoDB\\Collection::createIndex()`
57+
- :phpmethod:`MongoDB\\Collection::listIndexes()`
58+
- :manual:`2dsphere Indexes </core/2dsphere>` reference in the MongoDB
59+
manual
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
==========================================
2+
MongoDB\\Model\\IndexInfo::isGeoHaystack()
3+
==========================================
4+
5+
.. versionadded:: 1.4
6+
7+
.. default-domain:: mongodb
8+
9+
.. contents:: On this page
10+
:local:
11+
:backlinks: none
12+
:depth: 1
13+
:class: singlecol
14+
15+
Definition
16+
----------
17+
18+
.. phpmethod:: MongoDB\\Model\\IndexInfo::isGeoHaystack()
19+
20+
Return whether the index is a :manual:`geoHaystack
21+
</core/geohaystack>` index.
22+
23+
.. code-block:: php
24+
25+
function isGeoHaystack(): boolean
26+
27+
Return Values
28+
-------------
29+
30+
A boolean indicating whether the index is a geoHaystack index.
31+
32+
Examples
33+
--------
34+
35+
.. code-block:: php
36+
37+
<?php
38+
39+
$collection = (new MongoDB\Client)->selectCollection('test', 'places');
40+
41+
$collection->createIndex(['pos' => 'geoHaystack', 'x' => 1], ['bucketSize' => 5]);
42+
43+
foreach ($collection->listIndexes() as $index) {
44+
if ($index->isGeoHaystack()) {
45+
printf("%s has bucketSize: %d\n", $index->getName(), $index['bucketSize']);
46+
}
47+
}
48+
49+
The output would then resemble::
50+
51+
pos_geoHaystack_x_1 has bucketSize: 5
52+
53+
See Also
54+
--------
55+
56+
- :phpmethod:`MongoDB\\Collection::createIndex()`
57+
- :phpmethod:`MongoDB\\Collection::listIndexes()`
58+
- :manual:`geoHaystack Indexes </core/geohaystack>` reference in the MongoDB
59+
manual
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
===================================
2+
MongoDB\\Model\\IndexInfo::isText()
3+
===================================
4+
5+
.. versionadded:: 1.4
6+
7+
.. default-domain:: mongodb
8+
9+
.. contents:: On this page
10+
:local:
11+
:backlinks: none
12+
:depth: 1
13+
:class: singlecol
14+
15+
Definition
16+
----------
17+
18+
.. phpmethod:: MongoDB\\Model\\IndexInfo::isText()
19+
20+
Return whether the index is a :manual:`text </core/index-text>` index.
21+
22+
.. code-block:: php
23+
24+
function isText(): boolean
25+
26+
Return Values
27+
-------------
28+
29+
A boolean indicating whether the index is a text index.
30+
31+
Examples
32+
--------
33+
34+
.. code-block:: php
35+
36+
<?php
37+
38+
$collection = (new MongoDB\Client)->selectCollection('test', 'restaurants');
39+
40+
$collection->createIndex(['name' => 'text']);
41+
42+
foreach ($collection->listIndexes() as $index) {
43+
if ($index->isText()) {
44+
printf("%s has default language: %d\n", $index->getName(), $index['default_language']);
45+
}
46+
}
47+
48+
The output would then resemble::
49+
50+
name_text has default language: english
51+
52+
See Also
53+
--------
54+
55+
- :phpmethod:`MongoDB\\Collection::createIndex()`
56+
- :phpmethod:`MongoDB\\Collection::listIndexes()`
57+
- :manual:`Text Indexes </core/index-text>` reference in the MongoDB
58+
manual

‎src/Model/IndexInfo.php‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,26 @@ public function getVersion()
110110
return (integer) $this->info['v'];
111111
}
112112

113+
/**
114+
* Return whether or not this index is of type 2dsphere.
115+
*
116+
* @return boolean
117+
*/
118+
public function is2dSphere()
119+
{
120+
return array_search('2dsphere', $this->getKey(), true) !== false;
121+
}
122+
123+
/**
124+
* Return whether or not this index is of type geoHaystack.
125+
*
126+
* @return boolean
127+
*/
128+
public function isGeoHaystack()
129+
{
130+
return array_search('geoHaystack', $this->getKey(), true) !== false;
131+
}
132+
113133
/**
114134
* Return whether this is a sparse index.
115135
*
@@ -121,6 +141,16 @@ public function isSparse()
121141
return ! empty($this->info['sparse']);
122142
}
123143

144+
/**
145+
* Return whether or not this index is of type text.
146+
*
147+
* @return boolean
148+
*/
149+
public function isText()
150+
{
151+
return array_search('text', $this->getKey(), true) !== false;
152+
}
153+
124154
/**
125155
* Return whether this is a TTL index.
126156
*
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
namespace MongoDB\Tests\Model;
4+
5+
use MongoDB\Collection;
6+
use MongoDB\Tests\FunctionalTestCase;
7+
8+
class IndexInfoFunctionalTest extends FunctionalTestCase
9+
{
10+
private $collection;
11+
12+
public function setUp()
13+
{
14+
parent::setUp();
15+
16+
$this->collection = new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName());
17+
$this->collection->drop();
18+
}
19+
20+
public function tearDown()
21+
{
22+
if ($this->hasFailed()) {
23+
return;
24+
}
25+
26+
$this->collection->drop();
27+
}
28+
29+
public function testIs2dSphere()
30+
{
31+
$indexName = $this->collection->createIndex(['pos' => '2dsphere']);
32+
$result = $this->collection->listIndexes();
33+
34+
$result->rewind();
35+
$result->next();
36+
$index = $result->current();
37+
38+
$this->assertEquals($indexName, $index->getName());
39+
$this->assertTrue($index->is2dSphere());
40+
41+
$expectedVersion = version_compare($this->getServerVersion(), '3.2.0', '<') ? 2 : 3;
42+
$this->assertEquals($expectedVersion, $index['2dsphereIndexVersion']);
43+
}
44+
45+
public function testIsGeoHaystack()
46+
{
47+
$indexName = $this->collection->createIndex(['pos' => 'geoHaystack', 'x' => 1], ['bucketSize' => 5]);
48+
$result = $this->collection->listIndexes();
49+
50+
$result->rewind();
51+
$result->next();
52+
$index = $result->current();
53+
54+
$this->assertEquals($indexName, $index->getName());
55+
$this->assertTrue($index->isGeoHaystack());
56+
$this->assertEquals(5, $index['bucketSize']);
57+
}
58+
59+
public function testIsText()
60+
{
61+
$indexName = $this->collection->createIndex(['x' => 'text']);
62+
$result = $this->collection->listIndexes();
63+
64+
$result->rewind();
65+
$result->next();
66+
$index = $result->current();
67+
68+
$this->assertEquals($indexName, $index->getName());
69+
$this->assertTrue($index->isText());
70+
$this->assertEquals('english', $index['default_language']);
71+
$this->assertEquals('language', $index['language_override']);
72+
73+
$expectedVersion = version_compare($this->getServerVersion(), '3.2.0', '<') ? 2 : 3;
74+
$this->assertEquals($expectedVersion, $index['textIndexVersion']);
75+
76+
$this->assertSameDocument(['x' => 1], $index['weights']);
77+
}
78+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /