Laravel-MongoDB CRUD

Migrations

by Remy Pereira on 03rd November 2016

Eventhough MongoDB is schema-less, it is possible for you to create indices for efficient execution of queries. By default MongoDB creates an _id index for every collection. You could define additional indices that suit your purpose (for better performance).

With jenssegers laravel-mongodb-package (Moloquent) you get limited schema builder support. The supported operations are create, drop, collection, hasCollection, index and dropIndex, unique background, sparse and expire.

The following example explains how to create a members collection with partially predefined schema. You may create the migration file manually or use artisan.

To make a migration file for a collection called members

php artisan make:migration create_members_collection --create="members"
Created Migration: 2016_11_03_122343_create_members_collection

This will create a file called 2016_11_03_122343_create_members_collection in database/migrations/ with content like:


<?php

use IlluminateSupportFacadesSchema;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;

class CreateMembersCollection extends Migration
{
    public function up()
    {
        Schema::create('members', function (Blueprint $table) {
            
        });
    }

    public function down()
    {
        Schema::dropIfExists('members');
    }
}

The main changes to be made are

  1. Use Jenssegers\Mongodb\Schema\Blueprint instead of Illuminate\Database\Schema\Blueprint.
  2. Change $table to $collection.
  3. Redefine up() and down() functions.
  4. Inside up() and down() we use the create() and drop() methods from the package.


<?php

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


class CreateMembersCollection extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('members', function (Blueprint $collection) {
           $collection->index('name');
	   $collection->unique('email');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('members');
    }
}

This migration will create a collection called members with predefined indices name and email in addition to the default _id index.

Run the migration.

php artisan migrate
Migrated: 2016_11_03_124018_create_members_collection

Now you may verify the result from mongoshell.


> use library;
switched to db library

> show collections;
books
members
migrations

> db.members.getIndexes();
[
        {
                "v" : 1,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "library.members"
        },
        {
                "v" : 1,
                "key" : {
                        "name" : 1
                },
                "name" : "name_1",
                "ns" : "library.members"
        },
        {
                "v" : 1,
                "unique" : true,
                "key" : {
                        "email" : 1
                },
                "name" : "email_1",
                "ns" : "library.members"
        }
]

To rollback the migrations you can use artisan command,

php artisan migrate:rollback
Rolled back: 2016_11_03_124018_create_members_collection

This will remove the members collection.

Post a comment

Comments

Sreng-S | April 7, 2018 5:56 PM |

Great work :)

akram | January 11, 2018 9:26 AM |

hi,Tables are created but not all fields are created

Prasanna | January 8, 2018 7:40 AM |

HI i was configures db in mongo but when a migrate the file i got one error "sql state err" , please suggest some one solvation