模式构建器
概述
Laravel 提供了一个 门面 用于访问模式构建器类Schema
,让您创建和修改表格。外观是静态接口,用于类,使语法更简洁并提高可测试性。
Laravel集成支持Laravel Schema
外观中索引和集合管理方法的一部分。
要了解更多关于外观的信息,请参阅外观在Laravel文档中。
以下部分描述了Laravel集成中可用的Laravel schema构建器功能,并展示了如何使用它们的示例
注意
Laravel集成支持管理索引和集合,但不支持MongoDB JSON模式进行数据验证。要了解更多关于JSON模式验证的信息,请参阅服务器手册中的Schema Validation。
执行Laravel迁移
Laravel迁移允许您通过运行包含在Schema
外观中的方法来程序化地创建、修改和删除数据库模式。以下部分解释了在MongoDB数据库中如何编写迁移类以及如何运行它们。
在迁移中从内部修改数据库和集合提供了一种受控的方法,确保了应用程序中的一致性、版本控制和可逆性。
创建迁移类
您可以通过手动创建迁移类,或者使用php artisan make:migration
命令来生成它们。如果您生成它们,您必须对MongoDB数据库上的架构更改进行以下更改
如果迁移中引用了
Illuminate\Database\Schema\Blueprint
,请将其替换为MongoDB\Laravel\Schema\Blueprint
仅使用Laravel集成支持的命令和语法
提示
如果您的默认数据库连接设置为除MongoDB数据库之外的任何内容,请更新以下设置以确保迁移指定了正确的数据库
请确保您的
connections
数组项包含有效的mongodb
条目在您的config/database.php
文件中在迁移类的
$connection
字段中指定"mongodb"
以下示例迁移类包含以下方法
up()
,在运行迁移时创建集合和索引down()
,在回滚迁移时删除集合及其所有索引
declare(strict_types=1); use Illuminate\Database\Migrations\Migration; use Illuminate\Support\Facades\Schema; use MongoDB\Laravel\Schema\Blueprint; return new class extends Migration { protected $connection = 'mongodb'; /** * Run the migrations. */ public function up(): void { Schema::create('astronauts', function (Blueprint $collection) { $collection->index('name'); }); } /** * Reverse the migrations. */ public function down(): void { Schema::drop('astronauts'); } };
运行或回滚迁移
要从类文件运行数据库迁移,请替换占位符后运行以下命令
php artisan migrate --path=<path to your migration class file>
此命令在类文件中运行up()
函数,以在config/database.php
文件中指定的数据库中创建集合和索引。
要回滚迁移,请替换占位符后运行以下命令
php artisan migrate:rollback --path=<path to your migration class file>
此命令在类文件中运行down()
函数,以删除集合及其相关索引。
要了解更多关于Laravel迁移的信息,请参阅Laravel文档中的数据库:迁移。
检查集合是否存在
要检查集合是否存在,请在迁移文件中调用Schema
外观的hasCollection()
方法。您可以使用此方法有条件地执行迁移逻辑。
以下示例迁移在名为stars
的集合存在的情况下创建一个telescopes
集合。
$hasCollection = Schema::hasCollection('stars'); if ($hasCollection) { Schema::create('telescopes'); }
管理索引
MongoDB索引是提高查询效率的数据结构,通过减少检索查询结果所需的文档数量。某些索引,如地理空间索引,扩展了您查询数据的方式。
要使用索引提高查询性能,请确保索引覆盖了查询。有关索引和查询优化的更多信息,请参阅以下服务器手册条目
以下部分展示了如何使用模式构建器在集合上创建和删除各种类型的索引。
创建索引
要创建索引,请在迁移文件中的Schema
外观上调用create()
方法。传递给它集合名称和带有MongoDB\Laravel\Schema\Blueprint
参数的回调方法。在Blueprint
实例上指定索引创建详情。
以下示例迁移在以下集合字段上创建索引
在
mission_type
上的单字段索引在
launch_location
和launch_date
上的复合索引,在launch_date
上指定降序排序在
mission_id
字段上的唯一索引,指定索引名称为"unique_mission_id_idx"
点击查看输出按钮,查看运行迁移时创建的索引,包括_id
字段的默认索引
Schema::create('flights', function (Blueprint $collection) { $collection->index('mission_type'); $collection->index(['launch_location' => 1, 'launch_date' => -1]); $collection->unique('mission_id', options: ['name' => 'unique_mission_id_idx']); });
[ { v: 2, key: { _id: 1 }, name: '_id_' }, { v: 2, key: { mission_type: 1 }, name: 'mission_type_1' }, { v: 2, key: { launch_location: 1, launch_date: -1 }, name: 'launch_location_1_launch_date_-1' }, { v: 2, key: { mission_id: 1 }, name: 'unique_mission_id_idx', unique: true } ]
指定索引选项
MongoDB索引选项确定索引的使用和存储方式。您可以在调用索引创建方法(如index()
)时指定索引选项。
以下迁移代码显示了如何将校对添加到索引选项中。点击查看输出按钮,查看运行迁移时创建的索引,包括_id
字段的默认索引
Schema::create('passengers', function (Blueprint $collection) { $collection->index( 'last_name', name: 'passengers_collation_idx', options: [ 'collation' => [ 'locale' => 'de@collation=phonebook', 'numericOrdering' => true ], ], ); });
[ { v: 2, key: { _id: 1 }, name: '_id_' }, { v: 2, key: { last_name: 1 }, name: 'passengers_collation_idx', collation: { locale: 'de@collation=phonebook', caseLevel: false, caseFirst: 'off', strength: 3, numericOrdering: true, alternate: 'non-ignorable', maxVariable: 'punct', normalization: false, backwards: false, version: '57.1' } } ]
有关索引选项的更多信息,请参阅服务器手册中的所有索引类型的选项。
创建稀疏、TTL 和唯一索引
您可以使用 Laravel MongoDB 辅助方法创建以下类型的索引
稀疏索引,只允许包含指定字段的文档的索引条目
生存时间(TTL)索引,在设置的时间后过期
唯一索引,防止插入包含索引字段重复值的文档
要创建这些索引类型,请在迁移文件中的 Schema
门面调用 create()
方法。将集合名称和带有 MongoDB\Laravel\Schema\Blueprint
参数的回调方法传递给 create()
。在 Blueprint
实例上调用适当的辅助方法,并传递索引创建细节。
以下迁移代码展示了如何使用索引助手创建稀疏和 TTL 索引。点击 查看输出 按钮查看运行迁移后创建的索引,包括 _id
字段的默认索引
Schema::create('planets', function (Blueprint $collection) { $collection->sparse('rings'); $collection->expire('last_visible_dt', 86400); });
[ { v: 2, key: { _id: 1 }, name: '_id_' }, { v: 2, key: { rings: 1 }, name: 'rings_1', sparse: true }, { v: 2, key: { last_visible_dt: 1 }, name: 'last_visible_dt_1', expireAfterSeconds: 86400 } ]
您可以在索引选项中指定稀疏、TTL 和唯一索引,无论是单个字段还是组合索引
以下迁移代码展示了如何在单个字段上创建所有三种类型的索引。点击 查看输出 按钮查看运行迁移后创建的索引,包括 _id
字段的默认索引
Schema::create('planet_systems', function (Blueprint $collection) { $collection->index('last_visible_dt', options: ['sparse' => true, 'expireAfterSeconds' => 3600, 'unique' => true]); });
[ { v: 2, key: { _id: 1 }, name: '_id_' }, { v: 2, key: { last_visible_dt: 1 }, name: 'last_visible_dt_1', unique: true, sparse: true, expireAfterSeconds: 3600 } ]
有关这些索引的更多信息,请参阅服务器手册中的 索引属性。
创建地理空间索引
在MongoDB中,地理空间索引允许您查询包含、交叉和邻近性的地理空间坐标数据。
要创建地理空间索引,请在迁移文件中调用Schema
外观的create()
方法。将集合名称和带有MongoDB\Laravel\Schema\Blueprint
参数的回调方法传递给create()
。在Blueprint
实例上指定地理空间索引创建的详细信息。
以下示例迁移在spaceports
集合上创建了一个2d
和2dsphere
地理空间索引。点击查看输出按钮,查看运行迁移时创建的索引,包括_id
字段的默认索引。
Schema::create('spaceports', function (Blueprint $collection) { $collection->geospatial('launchpad_location', '2dsphere'); $collection->geospatial('runway_location', '2d'); });
[ { v: 2, key: { _id: 1 }, name: '_id_' }, { v: 2, key: { launchpad_location: '2dsphere' }, name: 'launchpad_location_2dsphere', '2dsphereIndexVersion': 3 }, { v: 2, key: { runway_location: '2d' }, name: 'runway_location_2d' } ]
有关地理空间索引的更多信息,请参阅服务器手册中的地理空间索引。
删除索引
要从集合中删除索引,请在迁移文件中调用Schema
外观的table()
方法。将其传递给表名和带有MongoDB\Laravel\Schema\Blueprint
参数的回调方法。在Blueprint
实例上调用带有索引名称的dropIndex()
方法。
注意
如果您删除了一个集合,MongoDB将自动删除与其相关的所有索引。
以下示例迁移从flights
集合中删除了一个名为unique_mission_id_idx
的索引。
Schema::table('flights', function (Blueprint $collection) { $collection->dropIndex('unique_mission_id_idx'); });