LoginSignup
1
0

More than 3 years have passed since last update.

Laravel MongoDBのmigrateで複合インデックスを設定

Posted at

概要

LaravelでMongoDBを使用する時に、jenssegers/laravel-mongodbというライブラリを使用するケースが多いと思います。このライブラリでもdbのmigrateは可能なのですが、複合インデックスが設定できるか試してみました。

参考

@komatzzさんのLaravel のマイグレーションで複合インデックスにしつつインデックス名を指定する方法
の記事を参考に、MongoDB用にマイグレーションファイルを編集して試してみました。

マイグレーションファイル

普通のインデックスとuniqueインデックスを設定。

2019_08_01_164256_initial_collections.php
<?php

use Jenssegers\Mongodb\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class InitialCollections extends Migration
{
    protected $connection = 'mongodb';

    public function up()
    {
        Schema::connection($this->connection)
            ->table('index_sample', function (Blueprint $collection)
            {
                $collection->index(['index1', 'index2']);
                $collection->unique(['unique1', 'unique2']);
            });
    }
}

実行結果

複合インデックスを設定できました。
スクリーンショット 2019-08-02 1.57.28.png

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0