Here we know before the installation. First of all you check that mongodb in your system exists or not. Then after check PHP version 7.2 or above. If available in your system it's good for you. You can Download from here and install it. and Install from here.
Now we move on the installation.
Here we need to install fresh Laravel 6 application. After installation follow steps.
Step 1 : Installation using composer
Now we must need to install one package into Laravel 6 application for MongoDB database.
composer require jenssegers/mongodb
Step 2 : Configuration
Open your project app/config.php file. Add this line in providers.
Jenssegers\Mongodb\MongodbServiceProvider::class,
Change your default database connection name in app/database.php
'default' => env('DB_CONNECTION', 'mysql'), // Mysql Replace to MongoDB 'default' => env('DB_CONNECTION', 'mongodb'),
Now add a new mongodb connection.
'mongodb' => [ 'driver' => 'mongodb', 'host' => env('MONGO_DB_HOST', 'localhost'), 'port' => env('MONGO_DB_PORT', 27017), 'database' => env('MONGO_DB_DATABASE'), 'username' => env('MONGO_DB_USERNAME'), 'password' => env('MONGO_DB_PASSWORD'), 'options' => [] ],
Step 3 : Open ROBO 3T
Open robo3t and configure it.
1) Create Connection wtih MongoDB database.
2) Create Database.
Step 4 : Set .env file variable.
Now we need to set database configuration into .env file.
#-------------------------------------------------------------# #--- Database Connecton --------------------------------------# #-------------------------------------------------------------# DB_CONNECTION=mongodb MONGO_DB_HOST=127.0.0.1 MONGO_DB_PORT=27017 MONGO_DB_DATABASE="Your database name" MONGO_DB_USERNAME MONGO_DB_PASSWORD
Step 5 : Put this line into each model file.
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
protected $collection = 'users_collection';
Example of user model like this.
app/User.php
<?php use Jenssegers\Mongodb\Eloquent\Model as Eloquent; class User extends Eloquent { protected $collection = 'users_collection'; }
If you want to user query bilder DB then you can set like this.
The driver plug right into the original query builder with MongoDB database.
When using MongoDB connections, you will be able to build fluent queries to perform MongoDB database operations.
For your benefit, there is a collection alias for table as well as some supplemental mongodb particular operations.
$users = DB::collection('users')->get(); $user = DB::collection('users')->where('email', 'contact@webappfix.com')->first();
If you did not change your default database connection, then you will need to particular id when querying.
$user = DB::connection('mongodb')->collection('users')->get();
Get All Documents
$users = User::all();
You can get single record using primary key
$user = User::find('517c44667HKl88101e05500f');
Where Constion
$users = User::where('read', '>', 100)->take(10)->get();
Now you can add multiple of record into MongoDB database without any limit.
MongoDB database data retrieval speed is good.
I hope it can help you.
Thanks....
We always thanks to you for reading our blogs.
Dharmesh Chauhan
(Swapinfoway Founder)Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us
Haresh Chauhan
(Co-Founder)We Are Also Recommending You :
- [FIXED] Unknown Collation: utf8mb4_0900_ai_ci In MYSQL
- Install PostgreSQL Ubuntu 20.04 With pgAdmin4
- SOLVED # 1118 Row size too large 8126. Changing some columns to TEXT or BLOB
- Unique violation: 7 ERROR: duplicate key value violates unique constraint "users_pkey" DETAIL: Key (id)=(1) already exists.
- Create Database In MYSQL Using PHP Script Example
- How To Install MongoDB In Ubuntu
- How To Improve SQL Query Performance
- Postgres Database Export
- How to See Pgsql Database List in Ubuntu
- MongoDB Database Connection In Laravel