@@ -30,7 +30,6 @@ available in {+odm-short+} and show examples of how to use them:
30
30
- :ref:`<laravel-eloquent-migrations>`
31
31
- :ref:`<laravel-eloquent-indexes>`
32
32
- :ref:`<laravel-eloquent-collections>`
33
- - :ref:`<laravel-eloquent-customize-collection>`
34
33
35
34
.. _laravel-eloquent-migrations:
36
35
@@ -50,7 +49,7 @@ You can create migration classes manually or generate them by using the
50
49
following changes to perform the schema changes on your MongoDB database:
51
50
52
51
- Replace the ``Illuminate\Database\Schema\Blueprint`` import with
53
- ``MongoDB\Laravel\Schema\Blueprint``
52
+ ``MongoDB\Laravel\Schema\Blueprint`` if it is referenced in your migration
54
53
- Specify ``mongodb`` in the ``$connection`` field
55
54
- Use only commands and syntax supported by {+odm-short+}
56
55
@@ -60,10 +59,12 @@ following changes to perform the schema changes on your MongoDB database:
60
59
file so that PHP artisan performs the migration on the correct database.
61
60
62
61
The following example migration class file that creates a collection and
63
- index when you run the migration and drops it when you roll back the migration:
62
+ index when you run the migration and drops it when you reverse, or roll back
63
+ the migration:
64
64
65
65
.. literalinclude:: /includes/schema-builder/astronauts_migration.php
66
66
:language: php
67
+ :emphasize-lines: 4, 10
67
68
68
69
Run or Roll Back a Migration
69
70
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -97,33 +98,259 @@ in the Laravel documentation.
97
98
Manage Indexes
98
99
--------------
99
100
100
- TODO
101
+ MongoDB indexes are data structures that improve query efficiency by reducing
102
+ the number of documents needed to retrieve results of a query. Indexes such as
103
+ geospatial indexes extend the ways in which you can query the data.
104
+
105
+ To improve query performance by using an index, make sure the query is
106
+ covered by the index. To learn more about indexes and query optimization, see
107
+ the following MongoDB server manual pages:
108
+
109
+ - :manual:`Indexes </indexes>`
110
+ - :manual:`Query Optimization </core/query-optimization/>`
111
+
112
+ The following sections show how you can use the schema builder to create and
113
+ drop various types of indexes on a collection.
114
+
115
+ Create an Index
116
+ ~~~~~~~~~~~~~~~
117
+
118
+ To create indexes, call the ``create()`` method on the ``Schema`` facade
119
+ in your migration file. Pass ``create()`` the collection name and a callback
120
+ method with a ``MongoDB\Laravel\Schema\Blueprint`` parameter. Specify the
121
+ index creation details on the ``Blueprint`` instance.
122
+
123
+ The following example migration creates indexes on the following collection
124
+ fields:
125
+
126
+ - Single field index on ``mission_type``
127
+ - Compound index on ``launch_location`` and ``launch_date``, specifying a descending sort order on ``launch_date``
128
+ - Unique index on the ``mission_id`` field, specifying the index name "unique_mission_id_idx"
129
+
130
+ Click the :guilabel:`VIEW OUTPUT` button to see the indexes created by running
131
+ the migration, including the default index on the ``_id`` field:
132
+
133
+
134
+ .. io-code-block::
135
+
136
+ .. input:: /includes/schema-builder/flights_migration.php
137
+ :language: php
138
+ :dedent:
139
+ :start-after: begin create index
140
+ :end-before: end create index
141
+
142
+ .. output::
143
+ :language: json
144
+ :visible: false
145
+
146
+ [
147
+ { v: 2, key: { _id: 1 }, name: '_id_' },
148
+ { v: 2, key: { mission_type: 1 }, name: 'mission_type_1' },
149
+ {
150
+ v: 2,
151
+ key: { launch_location: 1, launch_date: -1 },
152
+ name: 'launch_location_1_launch_date_-1'
153
+ },
154
+ {
155
+ v: 2,
156
+ key: { mission_id: 1 },
157
+ name: 'unique_mission_id_idx',
158
+ unique: true
159
+ }
160
+ ]
161
+
162
+
163
+ Specify Index Options
164
+ ~~~~~~~~~~~~~~~~~~~~~
165
+
166
+ MongoDB index options determine how the indexes are used and stored.
167
+ You can specify index options when calling an index creation method such
168
+ as ``index()`` on a ``Blueprint`` instance.
169
+
170
+ The following migration code shows how to add a collation to an index as an
171
+ index option. Click the :guilabel:`VIEW OUTPUT` button to see the indexes
172
+ created by running the migration, including the default index on the ``_id``
173
+ field:
174
+
175
+ .. io-code-block::
176
+
177
+ .. input:: /includes/schema-builder/passengers_migration.php
178
+ :language: php
179
+ :dedent:
180
+ :start-after: begin create index
181
+ :end-before: end create index
182
+
183
+ .. output:
184
+ :language: json
185
+ :visible: false
186
+
187
+ [
188
+ { v: 2, key: { _id: 1 }, name: '_id_' },
189
+ {
190
+ v: 2,
191
+ key: { last_name: 1 },
192
+ name: 'passengers_collation_idx',
193
+ collation: {
194
+ locale: 'de@collation=phonebook',
195
+ caseLevel: false,
196
+ caseFirst: 'off',
197
+ strength: 3,
198
+ numericOrdering: true,
199
+ alternate: 'non-ignorable',
200
+ maxVariable: 'punct',
201
+ normalization: false,
202
+ backwards: false,
203
+ version: '57.1'
204
+ }
205
+ }
206
+ ]
207
+
208
+
209
+ To learn more about index options, see :manual:`Options for All Index Types </reference/method/db.collection.createIndex/#options-for-all-index-types>`
210
+ in the MongoDB server manual.
211
+
212
+ Create Sparse, TTL, and Unique Indexes
213
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
214
+
215
+ You can use {+odm-short+} helper methods to create the following types of
216
+ indexes:
217
+
218
+ - Sparse indexes which allow index entries only for documents that contain the
219
+ specified field
220
+ - Time-to-live (TTL) indexes which expire after a set amount of time
221
+ - Unique indexes which prevent inserting documents that contain duplicate
222
+ values for the indexed field
223
+
224
+ To create these index types, call the ``create()`` method on the ``Schema`` facade
225
+ in your migration file. Pass ``create()`` the collection name and a callback
226
+ method with a ``MongoDB\Laravel\Schema\Blueprint`` parameter. Call the
227
+ appropriate helper method on the ``Blueprint`` instance and pass the
228
+ index creation details.
229
+
230
+ The following migration code shows how to create a sparse index and TTL index
231
+ by using the index helpers. Click the :guilabel:`VIEW OUTPUT` button to see
232
+ the indexes created by running the migration, including the default index on
233
+ the ``_id`` field:
234
+
235
+ .. io-code-block::
236
+
237
+ .. input:: /includes/schema-builder/planets_migration.php
238
+ :language: php
239
+ :dedent:
240
+ :start-after: begin index helpers
241
+ :end-before: end index helpers
242
+
243
+ .. output::
244
+ :language: json
245
+ :visible: false
246
+
247
+ [
248
+ { v: 2, key: { _id: 1 }, name: '_id_' },
249
+ { v: 2, key: { rings: 1 }, name: 'rings_1', sparse: true },
250
+ {
251
+ v: 2,
252
+ key: { last_visible_dt: 1 },
253
+ name: 'last_visible_dt_1',
254
+ expireAfterSeconds: 86400
255
+ }
256
+ ]
257
+
258
+ You can specify sparse, TTL, and unique indexes on a single field or compound
259
+ index by specifying them in the index options.
260
+
261
+ The following migration code shows how to create all three types of indexes
262
+ on a single field. Click the :guilabel:`VIEW OUTPUT` button to see the indexes
263
+ created by running the migration, including the default index on the ``_id``
264
+ field:
265
+
266
+ .. io-code-block::
267
+
268
+ .. input:: /includes/schema-builder/planets_migration.php
269
+ :language: php
270
+ :dedent:
271
+ :start-after: begin multi index helpers
272
+ :end-before: end multi index helpers
273
+
274
+ .. output::
275
+ :language: json
276
+ :visible: false
277
+
278
+ [
279
+ { v: 2, key: { _id: 1 }, name: '_id_' },
280
+ {
281
+ v: 2,
282
+ key: { last_visible_dt: 1 },
283
+ name: 'last_visible_dt_1',
284
+ unique: true,
285
+ sparse: true,
286
+ expireAfterSeconds: 3600
287
+ }
288
+ ]
289
+
290
+
291
+ To learn more about these indexes, see :manual:`Index Properties </core/indexes/index-properties/>`
292
+ in the MongoDB server manual.
293
+
294
+ Create a Geospatial Index
295
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
296
+
297
+ In MongoDB, geospatial indexes let you query geospatial coordinate data for
298
+ inclusion, intersection, and proximity.
299
+
300
+ To create geospatial indexes, call the ``create()`` method on the ``Schema`` facade
301
+ in your migration file. Pass ``create()`` the collection name and a callback
302
+ method with a ``MongoDB\Laravel\Schema\Blueprint`` parameter. Specify the
303
+ geospatial index creation details on the ``Blueprint`` instance.
304
+
305
+ The following example migration creates a ``2d`` and ``2dsphere`` geospatial
306
+ index on the ``spaceports`` collection:
307
+
308
+ .. literalinclude:: /includes/schema-builder/spaceports_migration.php
309
+ :language: php
310
+ :start-after: begin create geospatial index
311
+ :end-before: end create geospatial index
312
+
313
+ To learn more about geospatial indexes, see
314
+ :manual:`Geospatial Indexes </core/indexes/index-types/index-geospatial/>` in
315
+ the MongoDB Server manual.
316
+
317
+ Drop an Index
318
+ ~~~~~~~~~~~~~
319
+
320
+ You can drop indexes from a collection when you no longer need them.
321
+
322
+ When you no longer use an index, you can drop them
323
+
324
+ When you drop a collection, MongoDB automatically drops all the indexes
325
+ associated with it.
326
+
327
+ To drop an index, call the ``table()`` method on the ``Schema`` facade in your
328
+ migration file.
329
+
330
+ you must reference it by name.
331
+
332
+ The following example migration drops an index called ``unique_mission_id_idx``
333
+ from the ``flights`` collection:
334
+
335
+ .. literalinclude:: /includes/schema-builder/flights_migration.php
336
+ :language: php
337
+ :start-after: begin drop index
338
+ :end-before: end drop index
101
339
102
340
.. _laravel-eloquent-collections:
103
341
104
342
Manage Collections
105
343
------------------
106
344
345
+ Check if a collection exists: Schema::hasCollection(<collection name>)
346
+
107
347
TODO
108
348
109
349
Note that creating a collection is a noop unless it also creates an index
110
350
since MongoDB automatically creates collections when you first write to them.
111
351
112
- .. _laravel-eloquent-customize-collection:
113
-
114
- Customize Collection Creation
115
- -----------------------------
116
-
117
- TODO
118
-
119
352
Notes:
120
353
121
- - create and drop (is this different from index and dropIndex?)
122
- Schema::create(<collection name>, function ($collection) {
123
- $collection->index(<>);
124
- $collection->unique(<>);
125
- Geospatial index
126
-
127
354
- collection (what does this method do?)
128
355
Schema::hasCollection(<collection name>); check whether a collection exists
129
356
@@ -140,19 +367,11 @@ dropIndex($index = null)
140
367
public function dropIndexIfExists($indexOrColumns = null)\
141
368
public function hasIndex($indexOrColumns = null)
142
369
143
- public function unique($columns = null, $name = null, $algorithm = null, $options = [])
144
-
145
- Creation options
146
-
147
- https://www.mongodb.com/docs/manual/reference/method/db.collection.createIndex/#options-for-all-index-types
148
-
149
- - background - what is this? Hasn't this been replaced by index behavior in 4.2?
150
- - sparse https://www.mongodb.com/docs/manual/core/index-sparse/
151
- - expire is this a ttl index? https://www.mongodb.com/docs/manual/core/index-ttl/
152
- - geospatial https://www.mongodb.com/docs/manual/core/indexes/index-types/index-geospatial/
153
-
154
370
.. note::
155
371
156
- Although MongoDB collections can be schemaless, you can provide
157
- schemas for data validation. The {+odm-short+} schema builder does not
158
- currently offer methods to
372
+ Although MongoDB collections can be schemaless, they can use JSON schemas
373
+ for data validation. The {+odm-short+} schema builder does not offer methods
374
+ for data validation schemas. To learn more about JSON schema validation,
375
+ see :manual:`Schema Validation </core/schema-validation/>` in the
376
+ MongoDB server documentation.
377
+
0 commit comments