Introduction
MongoDB is one of the most popular NoSQL document-oriented databases. It features high write performance (ideal for analytics and IoT), high availability (automatic failover with replica sets), horizontal scalability (sharding), and a powerful query language (aggregation, full-text search, geospatial queries). Unlike SQL databases’ row and column format, each record in MongoDB is a document in BSON (Binary JSON) format. Your application retrieves this data in JSON format.When using MongoDB with Laravel, it is recommended to use the
mongodb/laravel-mongodb package, which is maintained by MongoDB. This package provides rich integration with Eloquent and various Laravel features.Installation
MongoDB PHP Driver
Themongodb PHP extension is required to connect to MongoDB. If you’re using Laravel Herd or php.new, it’s already installed. To install manually, use PECL:
Starting MongoDB Server
MongoDB Community Server can be used for local development. For installation instructions on Windows, macOS, Linux, and Docker, see the official installation guide. Using Docker:Installing the laravel-mongodb Package
Install themongodb/laravel-mongodb package using Composer:
Configuration
Environment Variables
Add MongoDB connection information to your.env file:
config/database.php
Add amongodb connection to the connections array in config/database.php:
Key Features
Eloquent Models
By extendingMongoDB\Laravel\Eloquent\Model, you can work with MongoDB using almost the same API as standard Eloquent models:
MongoDB is schema-less, so migrations are not required. Collections are created automatically when you save documents.
Basic CRUD Operations
You can use the same API as standard Eloquent:Arrays and Embedded Documents
You can work directly with MongoDB’s arrays and embedded documents:Query Builder
Use MongoDB’s query builder to write complex queries. For details, see the laravel-mongodb Query Builder documentation:Cache Driver
MongoDB’s cache driver uses TTL indexes to automatically delete expired entries. For details, see the Cache Driver documentation. Add a store toconfig/cache.php:
.env:
Queue Driver
You can use MongoDB as a queue driver. For details, see the Queue Driver documentation: Add a connection toconfig/queue.php:
.env:
File Storage with GridFS
Store files using MongoDB’s GridFS. Use the Flysystem GridFS adapter. For details, see the GridFS documentation:config/filesystems.php:
Using MySQL and MongoDB Together
Laravel allows you to use MySQL and MongoDB simultaneously. Specify the connection per model using the$connection property:
For relationships between MySQL and MongoDB models, see the Hybrid Relationships documentation.
Summary
Installation Checklist
Installation Checklist
- Install PHP extension with
pecl install mongodb - Start MongoDB server (locally, Docker, or Atlas)
- Install package with
composer require mongodb/laravel-mongodb - Set
MONGODB_URIandMONGODB_DATABASEin.env - Add
mongodbconnection toconfig/database.php
MongoDB vs MySQL
MongoDB vs MySQL
| Characteristic | MongoDB | MySQL |
|---|---|---|
| Data Model | Documents (JSON/BSON) | Tables (rows/columns) |
| Schema | Schema-less (flexible) | Fixed schema |
| Scaling | Horizontal scaling (easy) | Vertical scaling (primary) |
| Transactions | Multi-document support (v4+) | Full ACID support |
| Best For | Logs, analytics, IoT, flexible structures | Structured data, complex relations |
Feature Usage
Feature Usage
| Feature | Configuration |
|---|---|
| Eloquent Models | Extend MongoDB\Laravel\Eloquent\Model |
| Query Builder | DB::connection('mongodb')->collection(...) |
| Cache | config/cache.php + CACHE_STORE=mongodb |
| Queues | config/queue.php + QUEUE_CONNECTION=mongodb |
| File Storage | config/filesystems.php + GridFS adapter |
Next Steps
laravel-mongodb Official Docs
Full reference for Eloquent, query builder, relationships, and all features
Quick Start
Learn the basics of MongoDB and Laravel quickly
Database Configuration
Laravel database connection configuration basics
Eloquent Introduction
Introduction to Eloquent ORM basics