Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

899
Vistas
Laravel - Mongodb [ jenssegers/laravel-mongodb ] - Schema Builder

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!

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

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']);
    }
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda