Milind Daraniya

How to Use MongoDB in Laravel

Published July 19th, 2023 3 min read

Laravel, a popular PHP framework, provides seamless integration with various databases, including MongoDB—a NoSQL document-oriented database. With Laravel 10, integrating MongoDB into your Laravel applications has become even more straightforward. In this post, we will walk through the process of setting up MongoDB in Laravel 10 and provide practical examples to help you get started.

Section 1: Setting Up MongoDB in Laravel 10

Install Required Packages:

  • Start by installing the MongoDB PHP extension on your server. Consult the official MongoDB documentation for detailed installation instructions based on your operating system.
  • Next, use Composer to install the jenssegers/mongodb package, which provides the MongoDB integration for Laravel. Run the following command in your Laravel project directory:
composer require jenssegers/mongodb

Configure Database Connection:

  • Open the config/database.php file in your Laravel project and update the connections array.
  • Add a new connection configuration for MongoDB, specifying the necessary details such as host, port, database name, and authentication credentials, if required.

 

Section 2: Working with MongoDB in Laravel 10

Create MongoDB Models:

  • Create a new model that extends Jenssegers\Mongodb\Eloquent\Model instead of Laravel's base Model class.
  • Define the MongoDB collection associated with the model by setting the collection property.
  • Use the Eloquent syntax to define relationships, accessors, mutators, and other model functionality as needed.
<?php
namespace App\Models;

use Jenssegers\Mongodb\Eloquent\Model;

class Product extends Model
{
    protected $collection = 'products';

    // Define relationships, accessors, mutators, etc.
}

Perform CRUD Operations:

  • Use the Eloquent syntax to perform Create, Read, Update, and Delete operations on MongoDB collections.
  • Create a new document:
$product = new Product;
$product->name = 'Sample Product';
$product->price = 19.99;
$product->save();
  • Retrieve a document:
$product = Product::find($id);
  • Update a document:
$product = Product::find($id);
$product->price = 24.99;
$product->save();
  • Delete a document:
$product = Product::find($id);
$product->delete();

Querying MongoDB Data:

  • Use the familiar Eloquent syntax to query MongoDB data. For example:
$products = Product::where('price', '>', 10)->get();
  • You can also use MongoDB's query operators:
$products = Product::where('price', '>', 10)
                    ->orWhere('category', 'Electronics')
                    ->get();

Section 3: Advanced MongoDB Features in Laravel 10

Aggregations:

  • Laravel's MongoDB integration allows you to utilize MongoDB's powerful aggregation framework to perform advanced data manipulations, grouping, sorting, and calculations directly within the database.
  • Refer to the MongoDB documentation for details on constructing aggregation pipelines.

Geospatial Queries:

  • Leverage MongoDB's geospatial capabilities in Laravel to perform location-based queries and calculations.
  • Use Laravel's MongoDB integration to work seamlessly with geospatial data.