Skip to content

Commit b18b221

Browse files
committed
JM tech review 1
1 parent e227a7f commit b18b221

File tree

2 files changed

+98
-62
lines changed

2 files changed

+98
-62
lines changed

source/includes/usage-examples/index-code-examples.php

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,21 @@
22

33
require __DIR__ . '/../vendor/autoload.php';
44

5-
use MongoDB\Client;
5+
$uri = getenv('MONGODB_URI') ?: throw new RuntimeException('Set the MONGODB_URI variable to your Atlas URI that connects to the sample dataset');
6+
$client = new MongoDB\Client($uri);
67

7-
$client = new Client('<connection string>');
8-
$collection = $client-><database>-><collection>;
8+
$database = $client->sample_db;
9+
$collection = $database->sample_coll;
10+
11+
// start-to-json
12+
function toJSON(object $document): string
13+
{
14+
return MongoDB\BSON\Document::fromPHP($document)->toRelaxedExtendedJSON();
15+
}
16+
// end-to-json
917

1018
// start-single-field
11-
$result = $collection->createIndex(['<field name>' => 1]);
19+
$indexName = $collection->createIndex(['<field name>' => 1]);
1220
// end-single-field
1321

1422
// start-compound
@@ -18,49 +26,57 @@
1826
// end-compound
1927

2028
// start-multikey
21-
$result = $collection->createIndex(['<array field name>' => 1]);
29+
$indexName = $collection->createIndex(['<array field name>' => 1]);
2230
// end-multikey
2331

2432
// start-search-create
25-
$result = $collection->createSearchIndex(
33+
$indexName = $collection->createSearchIndex(
2634
['mappings' => ['dynamic' => true]],
2735
['name' => '<index name>']
2836
);
2937
// end-search-create
3038

3139
// start-search-list
3240
foreach ($collection->listSearchIndexes() as $indexInfo) {
33-
var_dump($indexInfo);
41+
echo toJSON($indexInfo) . PHP_EOL;
3442
}
3543
// end-search-list
3644

3745
// start-search-update
38-
$result = $collection->updateSearchIndex(
39-
['name' => '<index name>'],
40-
['mappings' => ['dynamic' => true]],
46+
$collection->updateSearchIndex(
47+
['name' => '<Search name>'],
48+
['mappings' => [
49+
'dynamic' => false,
50+
'fields' => [
51+
'<string field name>' => [
52+
'type' => 'string',
53+
'analyzer' => 'lucene.standard'
54+
]
55+
]
56+
]]
4157
);
4258
// end-search-update
4359

4460
// start-search-delete
45-
$result = $collection->dropIndex('<index name>');
61+
$collection->dropSearchIndex('<Search index name>');
4662
// end-search-delete
4763

4864
// start-text
49-
$result = $collection->createIndex(['<field name>' => 'text']);
65+
$indexName = $collection->createIndex(['<field name>' => 'text']);
5066
// end-text
5167

5268
// start-geo
53-
$result = $collection->createIndex(
54-
[ '<GeoJSON object field>' => '2dsphere'], ['name' => '<index name>']
69+
$indexName = $collection->createIndex(
70+
[ '<GeoJSON object field>' => '2dsphere']
5571
);
5672
// end-geo
5773

5874
// start-unique
59-
$result = $collection->createIndex(['<field name>' => 1], ['unique' => true]);
75+
$indexName = $collection->createIndex(['<field name>' => 1], ['unique' => true]);
6076
// end-unique
6177

6278
// start-wildcard
63-
$result = $collection->createIndex(['$**' => 1]);
79+
$indexName = $collection->createIndex(['$**' => 1]);
6480
// end-wildcard
6581

6682
// start-clustered
@@ -76,10 +92,10 @@
7692

7793
// start-list
7894
foreach ($collection->listIndexes() as $indexInfo) {
79-
var_dump($indexInfo);
95+
echo toJSON($indexInfo) . PHP_EOL;
8096
}
8197
// end-list
8298

8399
// start-remove
84-
$result = $collection->dropIndex('<index name>');
100+
$collection->dropIndex('<index name>');
85101
// end-remove

source/indexes.txt

Lines changed: 64 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ target namespace.
5353
:linenos:
5454
:emphasize-lines: 10-12
5555

56+
Some examples use the ``toJSON()`` function to represent change events, which are BSON
57+
documents, as Extended JSON. To use this function, paste the following code into your
58+
application file:
59+
60+
.. literalinclude:: /includes/usage-examples/index-code-examples.php
61+
:language: php
62+
:dedent:
63+
:start-after: start-to-json
64+
:end-before: end-to-json
65+
5666
Single Field Index
5767
------------------
5868

@@ -112,6 +122,9 @@ that has GeoJSON object values:
112122
:copyable:
113123
:dedent:
114124

125+
To learn more about the GeoJSON data type, see :manual:`GeoJSON Objects
126+
</reference/geojson/>` in the {+mdb-server+} manual.
127+
115128
.. TODO: To learn more about geospatial indexes, see the :ref:`php-geospatial-index`
116129
.. guide.
117130

@@ -164,12 +177,62 @@ ascending clustered index on the ``_id`` field:
164177
.. TODO: To learn more about clustered indexes, see the :ref:`php-clustered-index`
165178
.. guide.
166179

180+
Text Index
181+
----------
182+
183+
The following example creates a text index on the specified string field:
184+
185+
.. literalinclude:: /includes/usage-examples/index-code-examples.php
186+
:start-after: start-text
187+
:end-before: end-text
188+
:language: php
189+
:copyable:
190+
:dedent:
191+
192+
.. TODO: To learn more about text indexes, see the :ref:`php-text-index`
193+
.. guide.
194+
195+
List Indexes
196+
------------
197+
198+
The following example prints a list of indexes in the
199+
specified collection:
200+
201+
.. literalinclude:: /includes/usage-examples/index-code-examples.php
202+
:start-after: start-list
203+
:end-before: end-list
204+
:language: php
205+
:copyable:
206+
:dedent:
207+
208+
Delete an Index
209+
---------------
210+
211+
The following example deletes an index with the specified name:
212+
213+
.. literalinclude:: /includes/usage-examples/index-code-examples.php
214+
:start-after: start-remove
215+
:end-before: end-remove
216+
:language: php
217+
:copyable:
218+
:dedent:
219+
220+
.. TODO: To learn more about removing indexes, see :ref:`php-indexes-remove`
221+
.. in the Work with Indexes guide.
222+
167223
Atlas Search Index Management
168224
-----------------------------
169225

170226
The following sections contain code examples that describe how to manage
171227
:atlas:`Atlas Search indexes </atlas-search/manage-indexes/>`.
172228

229+
.. note:: Search Index Management is Asynchronous
230+
231+
The {+php-library+} manages Atlas Search indexes asynchronously. The
232+
library methods described in the following sections return the server
233+
response immediately, but the changes to your Search indexes take
234+
place in the background and might not complete until some time later.
235+
173236
.. To learn more about Atlas Search indexes, see the :ref:`php-atlas-search-index`
174237
.. guide.
175238

@@ -233,47 +296,4 @@ The following example deletes an Atlas Search index with the specified name:
233296
:dedent:
234297

235298
.. To learn more about deleting search indexes, see the :ref:`php-atlas-search-index-drop`
236-
.. guide.
237-
238-
Text Index
239-
----------
240-
241-
The following example creates a text index on the specified string field:
242-
243-
.. literalinclude:: /includes/usage-examples/index-code-examples.php
244-
:start-after: start-text
245-
:end-before: end-text
246-
:language: php
247-
:copyable:
248-
:dedent:
249-
250-
.. TODO: To learn more about text indexes, see the :ref:`php-text-index`
251-
.. guide.
252-
253-
List Indexes
254-
------------
255-
256-
The following example prints a list of indexes in the
257-
specified collection:
258-
259-
.. literalinclude:: /includes/usage-examples/index-code-examples.php
260-
:start-after: start-list
261-
:end-before: end-list
262-
:language: php
263-
:copyable:
264-
:dedent:
265-
266-
Delete an Index
267-
---------------
268-
269-
The following example deletes an index with the specified name:
270-
271-
.. literalinclude:: /includes/usage-examples/index-code-examples.php
272-
:start-after: start-remove
273-
:end-before: end-remove
274-
:language: php
275-
:copyable:
276-
:dedent:
277-
278-
.. TODO: To learn more about removing indexes, see :ref:`php-indexes-remove`
279-
.. in the Work with Indexes guide.
299+
.. guide.

0 commit comments

Comments
 (0)