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
0 commit comments