Skip to content

Commit 55a44c1

Browse files
authored
Merge pull request #127 from rustagir/DOCSP-41983-indexes-landing
DOCSP-41983: indexes landing pg
2 parents b9825a1 + cbc3879 commit 55a44c1

File tree

5 files changed

+423
-0
lines changed

5 files changed

+423
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
3+
require __DIR__ . '/../vendor/autoload.php';
4+
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);
7+
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
17+
18+
// start-single-field
19+
$indexName = $collection->createIndex(['<field name>' => 1]);
20+
// end-single-field
21+
22+
// start-compound
23+
$indexName = $collection->createIndex(
24+
['<field name 1>' => 1, '<field name 2>' => 1]
25+
);
26+
// end-compound
27+
28+
// start-multikey
29+
$indexName = $collection->createIndex(['<array field name>' => 1]);
30+
// end-multikey
31+
32+
// start-search-create
33+
$indexName = $collection->createSearchIndex(
34+
['mappings' => ['dynamic' => true]],
35+
['name' => '<Search index name>']
36+
);
37+
// end-search-create
38+
39+
// start-search-list
40+
foreach ($collection->listSearchIndexes() as $indexInfo) {
41+
echo toJSON($indexInfo) , PHP_EOL;
42+
}
43+
// end-search-list
44+
45+
// start-search-update
46+
$collection->updateSearchIndex(
47+
'<Search index name>',
48+
['mappings' => [
49+
'dynamic' => false,
50+
'fields' => [
51+
'<string field name>' => [
52+
'type' => 'string',
53+
'analyzer' => 'lucene.standard'
54+
]
55+
]
56+
]]
57+
);
58+
// end-search-update
59+
60+
// start-search-delete
61+
$collection->dropSearchIndex('<Search index name>');
62+
// end-search-delete
63+
64+
// start-text
65+
$indexName = $collection->createIndex(['<field name>' => 'text']);
66+
// end-text
67+
68+
// start-geo
69+
$indexName = $collection->createIndex(
70+
[ '<GeoJSON object field>' => '2dsphere']
71+
);
72+
// end-geo
73+
74+
// start-unique
75+
$indexName = $collection->createIndex(['<field name>' => 1], ['unique' => true]);
76+
// end-unique
77+
78+
// start-wildcard
79+
$indexName = $collection->createIndex(['$**' => 1]);
80+
// end-wildcard
81+
82+
// start-clustered
83+
$options = [
84+
'clusteredIndex' => [
85+
'key' => ['_id' => 1],
86+
'unique' => true
87+
]
88+
];
89+
90+
$database->createCollection('<collection name>', $options);
91+
// end-clustered
92+
93+
// start-list
94+
foreach ($collection->listIndexes() as $indexInfo) {
95+
echo $indexInfo;
96+
}
97+
// end-list
98+
99+
// start-remove
100+
$collection->dropIndex('<index name>');
101+
// 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+
$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);
7+
8+
$collection = $client->selectCollection('<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
@@ -14,6 +14,7 @@ MongoDB PHP Library
1414
/read
1515
/write
1616
/aggregation
17+
/indexes
1718
/tutorial
1819
/upgrade
1920
/reference

0 commit comments

Comments
 (0)