Docs Menu
Docs Home
/ /

Integrate MongoDB with Drupal

In this guide, you can learn how to create a Drupal content management system (CMS) that uses MongoDB as its database. Drupal is an open-source web CMS written in PHP that provides content authoring, composability, and authentication features.

Websites that provide personalized user experiences require robust capabilities for user authentication. Sites that have many authenticated users can experience performance issues when retrieving entity data from multiple tables in a relational database. MongoDB eliminates the need for complicated table joins and increases data retrieval speed for features such as personalized dashboards and dynamic content feeds.

When you use MongoDB as your site's database, Drupal stores entities as JSON documents that include all revisions, translations, and field data. This flexible data structure decreases latency and supports personalized, data-driven experiences for users.

MongoDB offers the following features that improve your Drupal sites:

  • Horizontal scaling: Distribute loads across multiple servers, making your database scalable for large user bases.

  • Integrated file storage: Store user files directly in the database instead of on the web server, simplifying hosting.

  • Full-text search: Avoid implementing separate search solutions by leveraging the MongoDB Search feature.

  • AI capabilities: Perform vector searches and integrate AI services by using the MongoDB Vector Search feature.

In this tutorial, you build a Drupal CMS that uses MongoDB to store your Drupal site's data. You create a locally hosted Drupal site, add an article to the site, and view the article's data in MongoDB.

Tip

This tutorial uses a local MongoDB replica set and a locally hosted site. To view a similar tutorial that uses a MongoDB Atlas M10 cluster and an AWS EC2 server, see the How to Run Drupal on MongoDB DEV article.

Follow the steps in this section to install the project dependencies, deploy a MongoDB replica set, and set up Drupal.

1

To create the Quick Start application, install the following software in your development environment:

Prerequisite
Notes

This is a PHP dependency management tool.

Follow the instructions that correspond to your operating system.

Install MongoDB locally to run a replica set.

Install mongodb, the MongoDB PHP extension. Follow the instructions in the extension documentation that correspond your operating system.

This tutorial uses the MongoDB Shell (mongosh) to view and interact with data. Select your operating system and installation method, then follow the instructions.

Use version 8.3 or later.

Terminal app and shell

For MacOS users, use Terminal or a similar app. For Windows users, use PowerShell or Command Prompt.

2

To use MongoDB with Drupal, you must run a MongoDB replica set. Perform the following steps to prepare and start a local three-node replica set:

  1. Increase your ulimit value to resolve system resource limitations.

    Drupal creates multiple files and indexes in a short period of time. From your terminal, run the following command to set the ulimit to 64000, which prevents your replica set from crashing:

    ulimit -n 64000
  2. Deploy your replica set.

    After increasing your ulimit, follow the instructions in the Deploy a Self-Managed Replica Set for Testing and Development tutorial to deploy and initialize a replica set. Ensure that your replica set is named rs0 and has three members running on ports 27017, 27018, and 27019.

3

Tip

Use the PHP Driver

If you prefer to create a database user by using the MongoDB PHP Driver instead of mongosh, pass the createUser command to the MongoDB\Database::command() method. To learn more, see Run a Database Command in the driver documentation.

Create a user with the necessary permissions for the Drupal database by performing the following steps.

  1. Run the following command from your terminal to connect to your replica set in mongosh:

    mongosh "mongodb://localhost:27017,localhost:27018,localhost:27019/?replicaSet=rs0"
  2. In the mongosh shell, run the following command to enter the admin database:

    use admin
  3. Then, run the following command from your shell to create a user:

    db.createUser({
    user: "<database username>",
    pwd: "<database user password>",
    roles: [
    { role: "readWrite", db: "drupal" },
    { role: "dbAdmin", db: "drupal" }
    ]
    })

    Replace <database username> and <database user password> with your preferred credentials. Save these values for use in future steps.

4

From a new terminal window, run the following commands to create a Drupal project named drupal-quickstart:

composer create-project drupal/recommended-project:11.1.5 drupal-quickstart
cd drupal-quickstart

Then, install the MongoDB module by running the following command:

composer require drupal/mongodb:^3.1

After installing Drupal, follow the steps in this section to allow your application to work with MongoDB.

1

The Drupal core PHP files require a patch to work with MongoDB. Navigate to your drupal-quickstart/web directory and apply the patch by running the following commands:

cd web
git apply -v modules/contrib/mongodb/patches/drupal-core-11.1.5.patch
cd ..
2

From your drupal-quickstart directory, navigate to the web/sites/default subdirectory and run the following commands:

cd web/sites/default
cp default.settings.php settings.php
chmod 777 settings.php
cd ../../..

These commands copy the default settings file into a new file named settings.php and update this file's permissions to allow Drupal to write new content.

3

To allow Drupal to modify your public files, create the files directory and set its permissions by running the following commands from your drupal-quickstart directory:

mkdir -p web/sites/default/files
chmod -R 777 web/sites/default/files

Then, specify the location of these files by verifying that your web/sites/default/settings.php file contains the following value:

$settings['file_public_path'] = 'sites/default/files';

Tip

If the preceding value is commented out in settings.php, uncomment it.

Follow the steps in this section to complete the Drupal installation by using the Drupal configuration wizard.

1

From your drupal-quickstart directory, run the following command to start the built-in PHP web server:

php -S localhost:8080 -t web

If successful, this command outputs the following information:

[Date Time] PHP 8.3.0 Development Server (http://localhost:8080) started
2

Open http://localhost:8080 in your web browser to launch the Drupal installation wizard. Then, configure your project by selecting the following responses.

Tip

If your site displays an error message about the deprecated non-canonical (boolean) cast, locate the following line in your project's DocParser.php file:

$this->ignoreNotImportedAnnotations = (boolean) $bool;

Replace this line with the following code:

$this->ignoreNotImportedAnnotations = (bool) $bool;
  1. On the Choose language page, select English and click Save and continue.

  2. On the Choose profile page, keep Standard selected and click Save and continue.

  3. On the Set up database page, select MongoDB as the database type and provide the following information:

    • Database name: drupal

    • Database username: The database username that you configured with the createUser command in a previous step

    • Database password: The database password that you configured with the createUser command in a previous step

    • SRV connection format: Leave unchecked

    • Database replica set: rs0

    • Host #1: localhost, port 27017

    • Host #2: localhost, port 27018

    • Host #3: localhost, port 27019

    Click Save and continue.

  4. Drupal installs the required modules. This might take several minutes.

  5. On the Configure site page, provide the following information:

    • Site name: Enter Drupal on MongoDB.

    • Site email address: Enter any email address. You can use a non-existent address.

    • Username: Choose an admin username.

    • Password: Choose a strong password.

    • Receive email notifications: Uncheck this box.

    Click Save and continue.

3

After installing Drupal and configuring your site, Drupal no longer requires write access to your settings files. To secure these files, run the following commands from your drupal-quickstart directory:

chmod 555 web/sites/default
chmod 444 web/sites/default/settings.php

After completing the installation, follow the steps in this section to create content and store its data in MongoDB.

1

After installation completes, your site redirects to the Drupal home page. Your site resembles the following image:

Screenshot of the Drupal home page after initial installation

Tip

Site Styling

If your Drupal site displays plain HTML without any styling, you might have to disable CSS and JavaScript aggregation. To disable aggregation, visit http://localhost:8080/admin/config/development/performance in your browser and uncheck the Aggregate CSS files and Aggregate JavaScript files boxes.

2

Click Content in the navigation bar at the top of the page. Then, click Add content > Article.

3

On the Create Article page, perform the following actions:

  1. In the Title section, enter "Welcome to My Drupal Site".

  2. In the Body section, enter the following text:

    This is my first article on Drupal with MongoDB!
    All content is stored as JSON documents in MongoDB.
  3. Select Menu settings and check the Provide a menu link box.

  4. Click Save.

4

After saving, you can see your published article. Your site resembles the following image:

Screenshot of the Drupal article page
5

Tip

Use the PHP Driver

If you prefer to view collections and content data by using the PHP driver instead of mongosh, use the MongoDB\Database::listCollections() and MongoDB\Collection::find() methods. To learn more, see the following PHP driver documentation:

To verify that your content is stored in MongoDB, perform the following steps:

  1. Open a new terminal window and connect to your MongoDB replica set in mongosh.

    Run the following command from your terminal:

    mongosh "mongodb://localhost:27017,localhost:27018,localhost:27019/?replicaSet=rs0"
  2. Enter the drupal database and view the collections.

    Run the following commands in your mongosh terminal:

    use drupal
    show collections

    Drupal creates multiple collections to store different types of content, including the following:

    • node: Stores content nodes

    • users: Stores user accounts

    • config: Stores configuration data

  3. View your article data.

    To view information about your article, run the following command in your mongosh terminal:

    db.node.find().pretty()
    [
    {
    _id: ObjectId('...'),
    nid: 1,
    vid: 1,
    type: 'article',
    uuid: '...',
    langcode: 'en',
    node_all_revisions: [
    {
    ...
    }
    ],
    node_current_revision: [
    {
    ...
    }
    ],
    node_latest_revision: [
    {
    ...
    title: 'Welcome to My Drupal Site',
    created: ...,
    changed: ...,
    promote: true,
    sticky: false,
    default_langcode: true,
    revision_default: true,
    revision_translation_affected: true,
    node_latest_revision__body: [
    {
    bundle: 'article',
    deleted: false,
    langcode: 'en',
    entity_id: 1,
    revision_id: 1,
    delta: 0,
    body_value: '<p>This is my first article on Drupal with MongoDB!<br>All content is stored as JSON documents in MongoDB.</p>',
    body_summary: '',
    body_format: 'basic_html'
    }
    ],
    node_latest_revision__comment: [
    {
    bundle: 'article',
    deleted: false,
    langcode: 'en',
    comment_status: 2,
    entity_id: 1,
    revision_id: 1,
    delta: 0
    }
    ]
    }
    ]
    }
    ]

    This command returns a JSON document that contains your article text and metadata.

Congratulations on completing the Drupal Quick Start tutorial!

After you complete these steps, you have a Drupal CMS that connects to your local MongoDB replica set and stores content as JSON documents.

To learn more about Drupal and MongoDB, view the following resources:

Back

Symfony Integration

On this page