Skip to content

Commit 88b6f0d

Browse files
Add documentation for MongoDB support (laravel#9945)
* Add mentions to MongoDB in the docs * formatting * Update session.md --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent dde9f07 commit 88b6f0d

File tree

7 files changed

+116
-5
lines changed

7 files changed

+116
-5
lines changed

Diff for: authentication.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ Want to get started fast? Install a [Laravel application starter kit](/docs/{{ve
5353
<a name="introduction-database-considerations"></a>
5454
### Database Considerations
5555

56-
By default, Laravel includes an `App\Models\User` [Eloquent model](/docs/{{version}}/eloquent) in your `app/Models` directory. This model may be used with the default Eloquent authentication driver. If your application is not using Eloquent, you may use the `database` authentication provider which uses the Laravel query builder.
56+
By default, Laravel includes an `App\Models\User` [Eloquent model](/docs/{{version}}/eloquent) in your `app/Models` directory. This model may be used with the default Eloquent authentication driver.
57+
58+
If your application is not using Eloquent, you may use the `database` authentication provider which uses the Laravel query builder. If your application is using MongoDB, check out MongoDB's official [Laravel user authentication documentation](https://www.mongodb.com/docs/drivers/php/laravel-mongodb/current/user-authentication/) .
5759

5860
When building the database schema for the `App\Models\User` model, make sure the password column is at least 60 characters in length. Of course, the `users` table migration that is included in new Laravel applications already creates a column that exceeds this length.
5961

Diff for: cache.md

+7
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,13 @@ In addition, you should ensure that values are provided for the DynamoDB cache s
111111
],
112112
```
113113

114+
<a name="mongodb"></a>
115+
#### MongoDB
116+
117+
If you are using MongoDB, a `mongodb` cache driver is provided by the official `mongodb/laravel-mongodb` package and can be configured using a `mongodb` database connection. MongoDB supports TTL indexes, which can be used to automatically clear expired cache items.
118+
119+
For more information on configuring MongoDB, please refer to the MongoDB [Cache and Locks documentation](https://www.mongodb.com/docs/drivers/php/laravel-mongodb/current/cache/).
120+
114121
<a name="cache-usage"></a>
115122
## Cache Usage
116123

Diff for: database.md

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ Almost every modern web application interacts with a database. Laravel makes int
2727

2828
</div>
2929

30+
Additionally, MongoDB is supported via the `mongodb/laravel-mongodb` package, which is officially maintained by MongoDB. Check out the [Laravel MongoDB](https://www.mongodb.com/docs/drivers/php/laravel-mongodb/) documentation for more information.
31+
3032
<a name="configuration"></a>
3133
### Configuration
3234

Diff for: documentation.md

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
- [Migrations](/docs/{{version}}/migrations)
6565
- [Seeding](/docs/{{version}}/seeding)
6666
- [Redis](/docs/{{version}}/redis)
67+
- [MongoDB](/docs/{{version}}/mongodb)
6768
- ## Eloquent ORM
6869
- [Getting Started](/docs/{{version}}/eloquent)
6970
- [Relationships](/docs/{{version}}/eloquent-relationships)

Diff for: mongodb.md

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# MongoDB
2+
3+
- [Introduction](#introduction)
4+
- [Installation](#installation)
5+
- [MongoDB Driver](#mongodb-driver)
6+
- [Starting a MongoDB Server](#starting-a-mongodb-server)
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.
15+
16+
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.
17+
18+
Before using MongoDB with Laravel, we recommend installing and using the `mongodb/laravel-mongodb` package via Composer. The `laravel-mongodb` package is officially maintained by MongoDB, and 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:
19+
20+
```shell
21+
composer require mongodb/laravel-mongodb
22+
```
23+
24+
<a name="installation"></a>
25+
## Installation
26+
27+
<a name="mongodb-driver"></a>
28+
### MongoDB Driver
29+
30+
To connect to a MongoDB database, the `mongodb` PHP extension is required. If you are developing locally using [Laravel Herd](https://herd.laravel.com) or installed PHP via `php.new`, you already have this extension installed on your system. However, if you need to install the extension manually, you may do so via PECL:
31+
32+
```shell
33+
pecl install mongodb
34+
```
35+
36+
For more information on installing the MongoDB PHP extension, check out the [MongoDB PHP extension installation instructions](https://www.php.net/manual/en/mongodb.installation.php).
37+
38+
<a name="starting-a-mongodb-server"></a>
39+
### Starting a MongoDB Server
40+
41+
The MongoDB Community Server can be used to run MongoDB locally and is available for installation on Windows, macOS, Linux, or as a Docker container. To learn how to install MongoDB, please refer to the [official MongoDB Community installation guide](https://docs.mongodb.com/manual/administration/install-community/).
42+
43+
The connection string for the MongoDB server can be set in your `.env` file:
44+
45+
```ini
46+
MONGODB_URI="mongodb://localhost:27017"
47+
MONGODB_DATABASE="laravel_app"
48+
```
49+
50+
For hosting MongoDB in the cloud, consider using [MongoDB Atlas](https://www.mongodb.com/cloud/atlas).
51+
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.
52+
53+
The connection string for MongoDB Atlas can also be set in your `.env` file:
54+
55+
```ini
56+
MONGODB_URI="mongodb+srv://<username>:<password>@<cluster>.mongodb.net/<dbname>?retryWrites=true&w=majority"
57+
MONGODB_DATABASE="laravel_app"
58+
```
59+
60+
<a name="install-the-laravel-mongodb-package"></a>
61+
### Install the Laravel MongoDB Package
62+
63+
Finally, use Composer to install the Laravel MongoDB package:
64+
65+
```shell
66+
composer require mongodb/laravel-mongodb
67+
```
68+
69+
> [!NOTE]
70+
> 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.
71+
72+
<a name="configuration"></a>
73+
## Configuration
74+
75+
You may configure your MongoDB connection via your application's `config/database.php` configuration file. Within this file, add a `mongodb` connection that utilizes the `mongodb` driver:
76+
77+
```php
78+
'connections' => [
79+
'mongodb' => [
80+
'driver' => 'mongodb',
81+
'dsn' => env('MONGODB_URI', 'mongodb://localhost:27017'),
82+
'database' => env('MONGODB_DATABASE', 'laravel_app'),
83+
],
84+
],
85+
```
86+
87+
<a name="features"></a>
88+
## Features
89+
90+
Once your configuration is complete, you can use the `mongodb` package and database connection in your application to leverage a variety of powerful features:
91+
92+
- [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.
93+
- [Write complex queries](https://www.mongodb.com/docs/drivers/php/laravel-mongodb/current/query-builder/) using the query builder.
94+
- The `mongodb` [cache driver](https://www.mongodb.com/docs/drivers/php/laravel-mongodb/current/cache/) is optimized to use MongoDB features such as TTL indexes to automatically clear expired cache entries.
95+
- [Dispatch and process queued jobs](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/), via 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+
To continue learning how to use MongoDB and Laravel, refer to MongoDB's [Quick Start guide](https://www.mongodb.com/docs/drivers/php/laravel-mongodb/current/quick-start/).

Diff for: queues.md

+1
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ The following dependencies are needed for the listed queue drivers. These depend
151151
- Amazon SQS: `aws/aws-sdk-php ~3.0`
152152
- Beanstalkd: `pda/pheanstalk ~5.0`
153153
- Redis: `predis/predis ~2.0` or phpredis PHP extension
154+
- [MongoDB](https://www.mongodb.com/docs/drivers/php/laravel-mongodb/current/queues/): `mongodb/laravel-mongodb`
154155

155156
</div>
156157

Diff for: session.md

+3-4
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ If you need to regenerate the session ID and remove all data from the session in
245245
## Session Blocking
246246

247247
> [!WARNING]
248-
> To utilize session blocking, your application must be using a cache driver that supports [atomic locks](/docs/{{version}}/cache#atomic-locks). Currently, those cache drivers include the `memcached`, `dynamodb`, `redis`, `database`, `file`, and `array` drivers. In addition, you may not use the `cookie` session driver.
248+
> To utilize session blocking, your application must be using a cache driver that supports [atomic locks](/docs/{{version}}/cache#atomic-locks). Currently, those cache drivers include the `memcached`, `dynamodb`, `redis`, `mongodb` (included in the official `mongodb/laravel-mongodb` package), `database`, `file`, and `array` drivers. In addition, you may not use the `cookie` session driver.
249249
250250
By default, Laravel allows requests using the same session to execute concurrently. So, for example, if you use a JavaScript HTTP library to make two HTTP requests to your application, they will both execute at the same time. For many applications, this is not a problem; however, session data loss can occur in a small subset of applications that make concurrent requests to two different application endpoints which both write data to the session.
251251

@@ -291,10 +291,9 @@ If none of the existing session drivers fit your application's needs, Laravel ma
291291
public function gc($lifetime) {}
292292
}
293293

294-
> [!NOTE]
295-
> Laravel does not ship with a directory to contain your extensions. You are free to place them anywhere you like. In this example, we have created an `Extensions` directory to house the `MongoSessionHandler`.
294+
Since Laravel does not include a default directory to house your extensions. You are free to place them anywhere you like. In this example, we have created an `Extensions` directory to house the `MongoSessionHandler`.
296295

297-
Since the purpose of these methods is not readily understandable, let's quickly cover what each of the methods do:
296+
Since the purpose of these methods is not readily understandable, here is an overview of the purpose of each method:
298297

299298
<div class="content-list" markdown="1">
300299

0 commit comments

Comments
 (0)