- MongoDB PHP Driver (Driver to make the application to work with MongoDB in PHP)
- MongoDB Community Edition (MongoDB installation).
- MongoDB Compass (GUI) (Graphic interface).
- Laravel MongoDB dependency (MongoDB package for installation on Laravel).
1 - Run the following command to install dependencies of repository.
composer install
2 - Create .env
file using the command:
cp .env.example .env
3 - Run the following command to generate API_KEY
value of .env
file.
php artisan key:generate
4.1 - Active MongoDB in your PC (I'm using distro Ubuntu of Linux, depending your operating system may be different) using the following command:
sudo systemctl start mongod
4.2 - Check if MongoDB is enabled.
sudo systemctl status mongod
4.3 - Case you want to disable.
sudo systemctl stop mongod
5 - When executing the migrations, is necessary use the commands to create some populated tables to some selection fields at forms.
php artisan migrate --seed
Or using the commands:
php artisan migrate
php artisan db:seed
6 - Execute Apache server
php artisan serve
1 - Create a Laravel project with compatible version to MongoDB database. Please, verify if your version Laravel application is compatible here.
composer create-project laravel/laravel project-name
2 - Install MongoDB dependecy.
composer require mongodb/laravel-mongodb
3 - Add the following line in config/app.php
.
'providers' => [
...
MongoDB\Laravel\MongoDBServiceProvider::class,
],
4 - Add and replace the following lines in config/database.php
.
4.1 - Replace:
'default' => env('DB_CONNECTION', 'mysql'),
4.2 - To:
'default' => env('DB_CONNECTION', 'mongodb'),
4.3 - On same file add mongodb
settings:
'connections' => [
...
'mongodb' => [
'driver' => 'mongodb',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', 27017),
'database' => env('DB_DATABASE', 'homestead'),
'username' => env('DB_USERNAME', 'homestead'),
'password' => env('DB_PASSWORD', 'secret'),
'options' => [
'appname' => 'homestead',
],
],
],
5 - Change the following informations in .env
.
DB_CONNECTION=mongodb
DB_HOST=127.0.0.1
DB_PORT=27017
DB_DATABASE=laravel-mongodb
DB_USERNAME=
DB_PASSWORD=
1 - Open the terminal and execute the following command to active MongoDB:
sudo systemctl start mongod
2 - Check if the MongoDB is working execute the following command.
sudo systemctl status mongod
4 - Open the MongoDB Compass and click on connect
.
5 - Open your database defined to you (in my case laravel-mongodb
).
6 - Verify existing collections.
8 - Open the terminal and execute the following command to export laravel-mongodb
database (Verify the name of your database):
mongodump --db laravel-mongodb
9 - All collections of database (in my case laravel-mongodb
) were exported.
10 - Back to home
page and you will notice the existence of dump
paste.
11 - Click on paste of your database (in my case laravel-mongodb
).
12 - All collections of database (in my case laravel-mongodb
) are saved in JSON
and BSON
files.
13 - Back to MongoDB Compass and delete your database clicking on trash
icon.
14 - Confirm the exclusition writing database name (in my case laravel-mongodb
).
15 - The database was deleted (in my case laravel-mongodb
).
16 - Open the terminal using the path of database export (in my case laravel-mongodb
) paste existing in dump
paste.
cd ~/dump/laravel-mongodb
Then you will have to insert the following command (Verify the name of database that you want to import, in my case laravel-mongodb
):
mongostore --db laravel-mongodb .
17 - The database was imported to MongoDB Compass.
18 - Return to MongoDB Compass and refresh
the database.
19 - Your database (in my case laravel-mongodb
) returned.
20 - Return to terminal and write the following command to desactive MongoDB.
sudo systemctl stop mongod
21 - Check if MongoDB was desactived.
sudo systemctl status mongod