4
votes

I have an web app with mongodb, using jenssegers package.

Since I'm using mongodb, do I need to create any migration table? Jenssegers database driver also has (limited) schema builder support

Schema::create('users', function($collection)
{
    $collection->index('name');

    $collection->unique('email');
});

I found two different "answers". This guy is using the schema builder

enter image description here

And the Mulkave answer:

Q: "When i run the comman "php artisan migrate" then i am getting following error where as migration table is created into mongodb database:"

A: "You might have misinterpreted the purpose of MongoDB being a document database, you do not need to have migrations in document-based databases and this is what they're good a. They have dynamic schemas so all you'll have to do is save your model regardless of what attributes they have which means you'll have to tighten your application's business logic to make sure the models are being saved as you expected."


So, do I need migrations table? Thanks!

1
mongo db is a document databse, it does not have any specific schema. for example, in your user collection one document (row equivalent in mysql) can have username field and another document can totally exclude username. it does not matter to mongo. so making migrations are redundant in my point of view.Sayantan Das

1 Answers

4
votes

I know this is a bit late, but I thought I'd throw in a bit of code here. I like to have an indexed model for two reasons:

  1. I can use it like an interface, ie I am forced to enter this information in because it's part of what the model requires. This ensures that I have a clean dataset. This is just preference, it's not a requirement.
  2. Indexed searches are faster than when they aren't index. I haven't done enough research into this to figure out if this is the case with jenssegers library. But it makes sense to me that, if you use a schema to set up a collection and set it to index these fields, then it would be faster when searching for records within this collection. You can also set this up manually from the Mongo side but I like that you can do it with this Eloquent extension.

So to the person reading this, if I've convinced you with this unverified argument and you'd like to set up a schema, I've found that it's easy to set up the collection but not easy to drop it. If you want to do a full migration (be aware that you'll lose all of your data when you do this) then you can use the code below:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateYourCollectionTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::connection('mongodb')->create('collection_name', function ($collection) {
            $collection->index('field_1');
            $collection->index('field_2');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        DB::connection('mongodb')->drop(['collection_name']);
    }
}