Skip to content

Commit f7e6f80

Browse files
author
Chris Cho
committed
wip
1 parent 4aa33a0 commit f7e6f80

File tree

3 files changed

+63
-41
lines changed

3 files changed

+63
-41
lines changed

docs/eloquent-models/schema-builder.txt

Lines changed: 55 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ MongoDB indexes are data structures that improve query efficiency by reducing
102102
the number of documents needed to retrieve results of a query. Indexes such as
103103
geospatial indexes extend the ways in which you can query the data.
104104

105-
To improve query performance by using an index, make sure the query is
105+
To improve query performance by using an index, make sure the query is
106106
covered by the index. To learn more about indexes and query optimization, see
107107
the following MongoDB server manual pages:
108108

@@ -116,7 +116,7 @@ Create an Index
116116
~~~~~~~~~~~~~~~
117117

118118
To create indexes, call the ``create()`` method on the ``Schema`` facade
119-
in your migration file. Pass ``create()`` the collection name and a callback
119+
in your migration file. Pass it the collection name and a callback
120120
method with a ``MongoDB\Laravel\Schema\Blueprint`` parameter. Specify the
121121
index creation details on the ``Blueprint`` instance.
122122

@@ -130,7 +130,6 @@ fields:
130130
Click the :guilabel:`VIEW OUTPUT` button to see the indexes created by running
131131
the migration, including the default index on the ``_id`` field:
132132

133-
134133
.. io-code-block::
135134

136135
.. input:: /includes/schema-builder/flights_migration.php
@@ -139,26 +138,25 @@ the migration, including the default index on the ``_id`` field:
139138
:start-after: begin create index
140139
:end-before: end create index
141140

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-
]
141+
.. output::
142+
:language: json
143+
:visible: false
161144

145+
[
146+
{ v: 2, key: { _id: 1 }, name: '_id_' },
147+
{ v: 2, key: { mission_type: 1 }, name: 'mission_type_1' },
148+
{
149+
v: 2,
150+
key: { launch_location: 1, launch_date: -1 },
151+
name: 'launch_location_1_launch_date_-1'
152+
},
153+
{
154+
v: 2,
155+
key: { mission_id: 1 },
156+
name: 'unique_mission_id_idx',
157+
unique: true
158+
}
159+
]
162160

163161
Specify Index Options
164162
~~~~~~~~~~~~~~~~~~~~~
@@ -177,8 +175,8 @@ field:
177175
.. input:: /includes/schema-builder/passengers_migration.php
178176
:language: php
179177
:dedent:
180-
:start-after: begin create index
181-
:end-before: end create index
178+
:start-after: begin index options
179+
:end-before: end index options
182180

183181
.. output:
184182
:language: json
@@ -287,7 +285,6 @@ field:
287285
}
288286
]
289287

290-
291288
To learn more about these indexes, see :manual:`Index Properties </core/indexes/index-properties/>`
292289
in the MongoDB server manual.
293290

@@ -303,12 +300,32 @@ method with a ``MongoDB\Laravel\Schema\Blueprint`` parameter. Specify the
303300
geospatial index creation details on the ``Blueprint`` instance.
304301

305302
The following example migration creates a ``2d`` and ``2dsphere`` geospatial
306-
index on the ``spaceports`` collection:
303+
index on the ``spaceports`` collection. Click the :guilabel:`VIEW OUTPUT`
304+
button to see the indexes created by running the migration, including the
305+
default index on the ``_id`` field:
306+
307+
.. io-code-block::
308+
.. input:: /includes/schema-builder/spaceports_migration.php
309+
:language: php
310+
:dedent:
311+
:start-after: begin create geospatial index
312+
:end-before: end create geospatial index
313+
314+
.. output::
315+
:language: json
316+
:visible: false
317+
318+
[
319+
{ v: 2, key: { _id: 1 }, name: '_id_' },
320+
{
321+
v: 2,
322+
key: { launchpad_location: '2dsphere' },
323+
name: 'launchpad_location_2dsphere',
324+
'2dsphereIndexVersion': 3
325+
},
326+
{ v: 2, key: { runway_location: '2d' }, name: 'runway_location_2d' }
327+
]
307328

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
312329

313330
To learn more about geospatial indexes, see
314331
:manual:`Geospatial Indexes </core/indexes/index-types/index-geospatial/>` in
@@ -317,17 +334,16 @@ the MongoDB Server manual.
317334
Drop an Index
318335
~~~~~~~~~~~~~
319336

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
337+
To drop indexes from a collection, call the ``table()`` method on the
338+
``Schema`` facade in your migration file. Pass it the table name and a
339+
callback method with a ``MongoDB\Laravel\Schema\Blueprint`` parameter.
340+
Call the ``dropIndex()`` method with the index name on the ``Blueprint``
341+
instance.
323342

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.
343+
.. note::
329344

330-
you must reference it by name.
345+
If you drop a collection, MongoDB automatically drops all the indexes
346+
associated with it.
331347

332348
The following example migration drops an index called ``unique_mission_id_idx``
333349
from the ``flights`` collection:

docs/includes/schema-builder/flights_migration.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use Illuminate\Database\Migrations\Migration;
44
use MongoDB\Laravel\Schema\Blueprint;
55
use Illuminate\Support\Facades\Schema;
6-
6+
77
return new class extends Migration
88
{
99

@@ -19,7 +19,7 @@ public function up(): void
1919
});
2020
// end create index
2121
}
22-
22+
2323
public function down(): void
2424
{
2525
// begin drop index

docs/includes/schema-builder/planets_migration.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ public function up(): void
1717
$collection->expire('last_visible_dt', 86400);
1818
});
1919
// end index helpers
20+
21+
// begin multi index helpers
22+
Schema::create('planet_systems', function (Blueprint $collection) {
23+
$collection->index('last_visible_dt', null, null, [ 'sparse' => true, 'expireAfterSeconds' => 3600, 'unique' => true]);
24+
});
25+
// end multi index helpers
2026
}
2127

2228
public function down(): void

0 commit comments

Comments
 (0)