|
| 1 | +# MongoDB |
| 2 | + |
| 3 | +- [Introduction](#introduction) |
| 4 | +- [Installation](#installation) |
| 5 | + - [Starting a MongoDB server](#starting-a-mongodb-server) |
| 6 | + - [MongoDB Driver](#mongodb-driver) |
| 7 | + - [Install the Laravel MongoDB package](#install-the-laravel-mongodb-package) |
| 8 | +- [Configuration](#configuration) |
| 9 | +- [Features](#features) |
| 10 | + |
| 11 | +<a name="introduction"></a> |
| 12 | +## Introduction |
| 13 | + |
| 14 | +[MongoDB](https://www.mongodb.com/resources/products/fundamentals/why-use-mongodb) is one of the most popular NoSQL document-oriented database, used for its high write load (useful for analytics or IoT) and high availability (easy to set replica sets with automatic failover). It can also shard the database easily for horizontal scalability and has a powerful query language for doing aggregation, text search or geospatial queries. Instead of storing data in tables of rows or columns like SQL databases, each record in a MongoDB database is a document described in BSON, a binary representation of the data. Applications can then retrieve this information in a JSON format. It supports a wide variety of data types, including documents, arrays, embedded documents, and binary data. |
| 15 | + |
| 16 | +Before using MongoDB with Laravel, we recommend installing and using the `mongodb/laravel-mongodb` package via Composer. While MongoDB is natively supported by PHP through the MongoDB driver, the [Laravel MongoDB](https://www.mongodb.com/docs/drivers/php/laravel-mongodb/) package provides a richer integration with Eloquent and other Laravel features. |
| 17 | + |
| 18 | +```shell |
| 19 | +composer require mongodb/laravel-mongodb |
| 20 | +``` |
| 21 | + |
| 22 | +<a name="installation"></a> |
| 23 | +## Installation |
| 24 | + |
| 25 | +<a name="starting-a-mongodb-server"></a> |
| 26 | +#### Starting a MongoDB server |
| 27 | + |
| 28 | +The MongoDB Community Server can be used to create a Laravel application. It is available for installation in Windows, macOS, Linux, or as Docker container. Read how to [Install MongoDB Community Edition](https://docs.mongodb.com/manual/administration/install-community/). |
| 29 | + |
| 30 | +The connection string for the MongoDB server can be set in the `.env` file: |
| 31 | + |
| 32 | +``` |
| 33 | +MONGODB_URI=mongodb://localhost:27017 |
| 34 | +MONGODB_DATABASE=laravel_app |
| 35 | +``` |
| 36 | + |
| 37 | +For hosting MongoDB in the cloud, consider using [MongoDB Atlas](https://www.mongodb.com/cloud/atlas). |
| 38 | +To access a MongoDB Atlas cluster locally from your application, you will need to [add your own IP address in the cluster's network settings](https://www.mongodb.com/docs/atlas/security/add-ip-address-to-list/) to the project's IP Access List. |
| 39 | + |
| 40 | +The connection string for MongoDB Atlas can be set in the `.env` file: |
| 41 | + |
| 42 | +``` |
| 43 | +MONGODB_URI=mongodb+srv://<username>:<password>@<cluster>.mongodb.net/<dbname>?retryWrites=true&w=majority |
| 44 | +MONGODB_DATABASE=laravel_app |
| 45 | +``` |
| 46 | + |
| 47 | +<a name="mongodb-driver"></a> |
| 48 | +#### MongoDB Driver |
| 49 | + |
| 50 | +To connect to a MongoDB database, the `mongodb` PHP extension is required. This extension can be installed using PECL: |
| 51 | + |
| 52 | +```shell |
| 53 | +pecl install mongodb |
| 54 | +``` |
| 55 | + |
| 56 | +Read the [MongoDB PHP extension installation instructions](https://www.php.net/manual/en/mongodb.installation.php) for more information. |
| 57 | + |
| 58 | +<a name="install-the-laravel-mongodb-package"></a> |
| 59 | +#### Install the Laravel MongoDB package |
| 60 | + |
| 61 | +Use Composer to install the Laravel MongoDB package: |
| 62 | + |
| 63 | +```shell |
| 64 | +composer require mongodb/laravel-mongodb |
| 65 | +``` |
| 66 | + |
| 67 | +> [!NOTE] |
| 68 | +> This installation of the package will fail if the `mongodb` PHP extension is not installed. The PHP configuration can differ between the CLI and the web server, so ensure the extension is enabled in both configurations. |
| 69 | +
|
| 70 | +<a name="configuration"></a> |
| 71 | +## Configuration |
| 72 | + |
| 73 | +You may configure your MongoDB connection via the `config/database.php` configuration file. |
| 74 | +Within this file, you can add a `mongodb` connection using the `mongodb` driver: |
| 75 | + |
| 76 | +```php |
| 77 | +'connections' => [ |
| 78 | + 'mongodb' => [ |
| 79 | + 'driver' => 'mongodb', |
| 80 | + 'dsn' => env('MONGODB_URI', 'mongodb://localhost:27017'), |
| 81 | + 'database' => env('MONGODB_DATABASE', 'laravel_app'), |
| 82 | + ], |
| 83 | +], |
| 84 | +``` |
| 85 | + |
| 86 | +<a name="features"></a> |
| 87 | +## Features |
| 88 | + |
| 89 | +Once your configuration is complete, you can use the `mongodb` connection in your application: |
| 90 | + |
| 91 | +- [Using Eloquent](https://www.mongodb.com/docs/drivers/php/laravel-mongodb/current/eloquent-models/), models can be stored in MongoDB collections. In addition to the standard Eloquent features, the Laravel MongoDB package provides additional features such as embedded relationships. The package also provides direct access to the MongoDB driver, which can be used to execute operations such as raw queries and aggregation pipelines. |
| 92 | +- [Write complex queries](https://www.mongodb.com/docs/drivers/php/laravel-mongodb/current/query-builder/) using the query builder. |
| 93 | +- [Using the Cache](https://www.mongodb.com/docs/drivers/php/laravel-mongodb/current/cache/), the `mongodb` cache driver is optimized to use MongoDB features such as TTL indexes to automatically clear expired cache entries. |
| 94 | +- Using the Session with Laravel's `cache` and the `mongodb` cache driver. |
| 95 | +- [Using the Queue](https://www.mongodb.com/docs/drivers/php/laravel-mongodb/current/queues/) with the `mongodb` queue driver. |
| 96 | +- [Storing files in GridFS](https://www.mongodb.com/docs/drivers/php/laravel-mongodb/current/gridfs/), by way of the [GridFS Adapter for Flysystem](https://flysystem.thephpleague.com/docs/adapter/gridfs/). |
| 97 | +- Most third party packages using a database connection or Eloquent can be used with MongoDB. |
| 98 | + |
| 99 | +Refer to the [**Quick Start**](https://www.mongodb.com/docs/drivers/php/laravel-mongodb/current/quick-start/) guide to connect a Laravel application to MongoDB, and perform read/write operations on the data. |
0 commit comments