Skip to content

Commit a2c7296

Browse files
committed
DOCSP-41983: indexes landing pg
1 parent cd4c81f commit a2c7296

File tree

5 files changed

+362
-0
lines changed

5 files changed

+362
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
require __DIR__ . '/../vendor/autoload.php';
4+
5+
use MongoDB\Client;
6+
7+
$client = new Client('<connection string>');
8+
$collection = $client-><database>-><collection>;
9+
10+
// start-single-field
11+
$result = $collection->createIndex(['<field name>' => 1]);
12+
// end-single-field
13+
14+
// start-compound
15+
$indexName = $collection->createIndex(
16+
['<field name 1>' => 1, '<field name 2>' => 1]
17+
);
18+
// end-compound
19+
20+
// start-multikey
21+
$result = $collection->createIndex(['<array field name>' => 1]);
22+
// end-multikey
23+
24+
// start-search-create
25+
$result = $collection->createSearchIndex(
26+
['mappings' => ['dynamic' => true]],
27+
['name' => '<index name>']
28+
);
29+
// end-search-create
30+
31+
// start-search-list
32+
foreach ($collection->listIndexes() as $indexInfo) {
33+
var_dump($indexInfo);
34+
}
35+
// end-search-list
36+
37+
// start-search-update
38+
$result = $collection->updateSearchIndex(
39+
['name' => '<index name>'],
40+
['mappings' => ['dynamic' => true]],
41+
);
42+
// end-search-update
43+
44+
// start-search-delete
45+
$result = $collection->dropIndex('<index name>');
46+
// end-search-delete
47+
48+
// start-text
49+
$result = $collection->createIndex(['<field name>' => 'text']);
50+
// end-text
51+
52+
// start-geo
53+
$result = $collection->createIndex(
54+
[ '<GeoJSON object field>' => '2dsphere'], ['name' => '<index name>']
55+
);
56+
// end-geo
57+
58+
// start-unique
59+
$result = $collection->createIndex(['<field name>' => 1], ['unique' => true]);
60+
// end-unique
61+
62+
// start-wildcard
63+
$result = $collection->createIndex(['$**' => 1]);
64+
// end-wildcard
65+
66+
// start-clustered
67+
$options = [
68+
'clusteredIndex' => [
69+
'key' => ['_id' => 1],
70+
'unique' => true
71+
]
72+
];
73+
74+
$database->createCollection('<collection name>', $options);
75+
// end-clustered
76+
77+
// start-remove
78+
$result = $collection->dropIndex('<index name>');
79+
// end-remove
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Sample Application
2+
~~~~~~~~~~~~~~~~~~
3+
4+
You can use the following sample application to test the code examples on this
5+
page. To use the sample application, perform the following steps:
6+
7+
1. Ensure you have the {+php-library+} installed in your project.
8+
#. Copy the following code and paste it into a new ``.php`` file.
9+
#. Copy a code example from this page and paste it on the specified
10+
lines in the file.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
require __DIR__ . '/../vendor/autoload.php';
4+
5+
use MongoDB\Client;
6+
7+
$client = new Client('<connection string>');
8+
$collection = $client-><database>-><collection>;
9+
10+
// Start example code here
11+
12+
// End example code here

source/index.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ MongoDB PHP Library
1212

1313
Get Started </get-started>
1414
/read
15+
/indexes
1516
/tutorial
1617
/upgrade
1718
/reference

source/indexes.txt

Lines changed: 260 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,260 @@
1+
.. _php-indexes:
2+
3+
=================================
4+
Optimize Queries by Using Indexes
5+
=================================
6+
7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 2
11+
:class: singlecol
12+
13+
.. facet::
14+
:name: genre
15+
:values: reference
16+
17+
.. meta::
18+
:description: Learn how to use indexes by using the MongoDB PHP Library.
19+
:keywords: query, optimization, efficiency, usage example, code example
20+
21+
.. .. toctree::
22+
.. :titlesonly:
23+
.. :maxdepth: 1
24+
..
25+
.. /work-with-indexes
26+
27+
Overview
28+
--------
29+
30+
On this page, you can see copyable code examples that show how to manage
31+
different types of indexes by using the {+php-library+}.
32+
33+
.. .. tip::
34+
..
35+
.. To learn more about working with indexes, see the :ref:`php-work-indexes`
36+
.. guide. To learn more about any of the indexes shown on this page, see the link
37+
.. provided in each section.
38+
39+
To use an example from this page, copy the code example into the
40+
:ref:`sample application <php-index-sample>` or your own application.
41+
Be sure to replace all placeholders in the code examples, such as
42+
``<connection string>``, with the relevant values for your MongoDB
43+
deployment.
44+
45+
.. _php-index-sample:
46+
47+
.. include:: /includes/usage-examples/sample-app-intro.rst
48+
49+
.. literalinclude:: /includes/usage-examples/sample-app.php
50+
:language: php
51+
:copyable:
52+
:linenos:
53+
:emphasize-lines: 10-12
54+
55+
Single Field Index
56+
------------------
57+
58+
The following example creates an ascending index on the specified field:
59+
60+
.. literalinclude:: /includes/usage-examples/index-code-examples.php
61+
:start-after: start-single-field
62+
:end-before: end-single-field
63+
:language: php
64+
:copyable:
65+
:dedent:
66+
67+
.. To learn more about single field indexes, see the
68+
.. :ref:`php-single-field-index` guide.
69+
70+
Compound Index
71+
--------------
72+
73+
The following example creates a compound index on the specified fields:
74+
75+
.. literalinclude:: /includes/usage-examples/index-code-examples.php
76+
:start-after: start-compound
77+
:end-before: end-compound
78+
:language: php
79+
:copyable:
80+
:dedent:
81+
82+
.. To learn more about compound indexes, see the :ref:`php-compound-index` guide.
83+
84+
Multikey Index
85+
--------------
86+
87+
The following example creates a multikey index on the specified array-valued field:
88+
89+
.. literalinclude:: /includes/usage-examples/index-code-examples.php
90+
:start-after: start-multikey
91+
:end-before: end-multikey
92+
:language: php
93+
:copyable:
94+
:dedent:
95+
96+
.. TODO To learn more about multikey indexes, see the :ref:`php-multikey-index`
97+
.. guide.
98+
99+
Geospatial Index
100+
----------------
101+
102+
The following example creates a 2dsphere index on the specified field
103+
that has GeoJSON object values:
104+
105+
.. literalinclude:: /includes/usage-examples/index-code-examples.php
106+
:start-after: start-geo
107+
:end-before: end-geo
108+
:language: php
109+
:copyable:
110+
:dedent:
111+
112+
.. TODO: To learn more about geospatial indexes, see the :ref:`php-geospatial-index`
113+
.. guide.
114+
115+
Unique Index
116+
------------
117+
118+
The following example creates a unique index on the specified field:
119+
120+
.. literalinclude:: /includes/usage-examples/index-code-examples.php
121+
:start-after: start-unique
122+
:end-before: end-unique
123+
:language: php
124+
:copyable:
125+
:dedent:
126+
127+
.. TODO: To learn more about unique indexes, see the :ref:`php-unique-index`
128+
.. guide.
129+
130+
Wildcard Index
131+
--------------
132+
133+
The following example creates a wildcard index in the specified collection:
134+
135+
.. literalinclude:: /includes/usage-examples/index-code-examples.php
136+
:start-after: start-wildcard
137+
:end-before: end-wildcard
138+
:language: php
139+
:copyable:
140+
:dedent:
141+
142+
.. TODO: To learn more about wildcard indexes, see the :ref:`php-wildcard-index`
143+
.. guide.
144+
145+
Clustered Index
146+
---------------
147+
148+
You can create a clustered index when creating a new collection in a
149+
specified database. The following example creates a new collection with a
150+
clustered index on the ``_id`` field:
151+
152+
.. literalinclude:: /includes/usage-examples/index-code-examples.php
153+
:start-after: start-clustered
154+
:end-before: end-clustered
155+
:language: php
156+
:copyable:
157+
:dedent:
158+
159+
.. TODO: To learn more about clustered indexes, see the :ref:`php-clustered-index`
160+
.. guide.
161+
162+
Atlas Search Index Management
163+
-----------------------------
164+
165+
The following sections contain code examples that describe how to manage
166+
:atlas:`Atlas Search indexes </atlas-search/manage-indexes/>`.
167+
168+
.. To learn more about Atlas Search indexes, see the :ref:`php-atlas-search-index`
169+
.. guide.
170+
171+
Create Search Index
172+
~~~~~~~~~~~~~~~~~~~
173+
174+
The following example creates an Atlas Search index on the specified field:
175+
176+
.. literalinclude:: /includes/usage-examples/index-code-examples.php
177+
:start-after: start-search-create
178+
:end-before: end-search-create
179+
:language: php
180+
:copyable:
181+
:dedent:
182+
183+
.. To learn more about creating search indexes, see the
184+
.. :ref:`php-atlas-search-index-create` guide.
185+
186+
List Search Indexes
187+
~~~~~~~~~~~~~~~~~~~
188+
189+
The following example prints a list of Atlas Search indexes in the specified collection:
190+
191+
.. literalinclude:: /includes/usage-examples/index-code-examples.php
192+
:start-after: start-search-list
193+
:end-before: end-search-list
194+
:language: php
195+
:copyable:
196+
:dedent:
197+
198+
.. To learn more about listing search indexes, see the :ref:`php-atlas-search-index-list`
199+
.. guide.
200+
201+
Update Search Indexes
202+
~~~~~~~~~~~~~~~~~~~~~
203+
204+
The following example updates an existing Atlas Search index with the specified
205+
new index definition:
206+
207+
.. literalinclude:: /includes/usage-examples/index-code-examples.php
208+
:start-after: start-search-update
209+
:end-before: end-search-update
210+
:language: php
211+
:copyable:
212+
:dedent:
213+
214+
.. To learn more about updating search indexes, see the :ref:`php-atlas-search-index-update`
215+
.. guide.
216+
217+
Delete Search Indexes
218+
~~~~~~~~~~~~~~~~~~~~~
219+
220+
The following example deletes an Atlas Search index with the specified name:
221+
222+
.. literalinclude:: /includes/usage-examples/index-code-examples.php
223+
:start-after: start-search-delete
224+
:end-before: end-search-delete
225+
:language: php
226+
:copyable:
227+
:dedent:
228+
229+
.. To learn more about deleting search indexes, see the :ref:`php-atlas-search-index-drop`
230+
.. guide.
231+
232+
Text Index
233+
----------
234+
235+
The following example creates a text index on the specified string field:
236+
237+
.. literalinclude:: /includes/usage-examples/index-code-examples.php
238+
:start-after: start-text
239+
:end-before: end-text
240+
:language: php
241+
:copyable:
242+
:dedent:
243+
244+
.. TODO: To learn more about text indexes, see the :ref:`php-text-index`
245+
.. guide.
246+
247+
Delete an Index
248+
---------------
249+
250+
The following example deletes an index with the specified name:
251+
252+
.. literalinclude:: /includes/usage-examples/index-code-examples.php
253+
:start-after: start-remove
254+
:end-before: end-remove
255+
:language: php
256+
:copyable:
257+
:dedent:
258+
259+
.. TODO: To learn more about removing indexes, see :ref:`php-indexes-remove`
260+
.. in the Work with Indexes guide.

0 commit comments

Comments
 (0)