This project is not covered by Drupal’s security advisory policy.

This module allows you to store content entities in a MongoDB database. By extending specific classes provided by the module, you can leverage MongoDB for scalable and high-performance content storage. It provides no interface but easy entity's annotation replacement and some configurations.

It does not YET provide anyway to move or configure nodes into mongoDB. But you can use it for new entities to integrate seamlessly in your new or existing project.

Revisioning is also not yet properly implemented. It is in the TODO

Features

  • Seamless transition from SQL to MongoDB for content entities.
  • Support for string-based IDs in URLs, if using forms.
  • Easily configurable MongoDB indexes within entity annotations.
  • Supports aggregations
  • Views are currently not supported due to SQL-based storage expectations.

It tries to leverage the same Entity Query Interface used for SQL but aggregation just has a simple aggregate() function that can be used to run pipelines. The module is still under development, so if you find any issues or have ideas, please create an issue!

Installation

  • Ensure you have the mongodb dependency installed.
  • Create or choose an existing content entity.
  • Extend your entity storage from Drupal\mongo_content_entity_type\Storage\MongoContentEntityTypeStorage.
  • Extend your entity class from Drupal\mongo_content_entity_type\Entity\MongoContentEntityBase.
  • Extend the entity interface from Drupal\mongo_content_entity_type\Entity\MongoContentEntityBaseInterface.
  • (Optional) If using forms, extend MongoHtmlRouteProvider instead of AdminHtmlRouteProvider.
  • For Views, remove the views_data class as it's not supported.

Extend entity storage from within the entity's annotation

* @ContentEntityType(
 ..........................
 *   handlers = {
 *     "storage" = "Drupal\mongo_content_entity_type\Storage\MongoContentEntityTypeStorage",*
 *   },
 * ......................
 *   entity_keys = {
 *     "id" = "_id",
 *    ....................
 *   },
 *   mongo_indexes_fields = {
 *   ................
 *   }
 * )
 */

Entity Class, just extend the class from MongoContentEntityBase

use Drupal\mongo_content_entity_type\Entity\MongoContentEntityBase;

class Activity extends MongoContentEntityBase implements ActivityInterface {

  use EntityChangedTrait;
  use EntityPublishedTrait;
.......
........

Entity Class Interface, just extend the class from MongoContentEntityBaseInterface

use Drupal\mongo_content_entity_type\Entity\MongoContentEntityBaseInterface;

interface ActivityInterface extends MongoContentEntityBaseInterface, EntityChangedInterface, EntityPublishedInterface, EntityOwnerInterface {
.....
.....
.....

Update your entity's annotation the id-key from "id" to "_id"

 *   entity_keys = {
 *     "id" = "_id",
 *     ...
 *     ...
 *   },

At the end of the annotation, if you require indexes to be created, add the them like so

*   mongo_indexes_fields = {
 *      "string_indexes" = {
 *          "name" = "text",
 *          "description" = "text"
 *      },
 *      "numeric_indexes" = {
 *          "pid" = {
 *                "value" = -1,
 *                "extra" = {
 *                    "unique" = 1
 *                 }
 *            },
 *          "created" = -1,
 *          "ip_address" = 1
 *     }
 *     "compound_indexes" = {
 *        "cmp_1" = {
 *          "fields" = {
 *              "user_id" = -1,
 *              "source_id" = 1
 *          }
 *        }
 *     }
 *   }

After installation, navigate to your settings.php MongoDB module configuration to add your database to the list of "entities" databases.

$settings['mongodb'] = [
  'clients' => [
    ....
  ],
  'databases' => [
    .....
    'entities' => ['default', "YOUR_DATABASE_NAME"],
  ],
];

Additional Requirements

You need to configure first the main MongoDB module

No additional libraries

Community Documentation

TODO. Soon I will try add detailed documentation but below are simple examples.

Simple Entity Interface API, just like the regular way of accessing it

$entities = \Drupal::entityTypeManager()->getStorage('YOUR_ENTITY_TYPE')
      ->loadMultiple($ids);

Aggregation example. It is different from the SQL Aggregation. aggregate() accepts two parameters, stage as a string and query as an array. It is sufficient to compose any type of pipeline.

// Initialize query and apply conditions
$query = \Drupal::entityQueryAggregate('YOUR_ENTITY_TYPE')...
// or
$query = mongo_get_query_aggregate('YOUR_ENTITY_TYPE')
  ->aggregate('$match', $filter_conditions)
  
  // Add a 'facet' stage to process metadata and ids
  ->aggregate('$facet', [
    'metadata' => [['$group' => ['_id' => null, 'total' => ['$sum' => 1]]]],
    'ids' => [
      ['$sort' => ['created' => -1]],
      ['$skip' => (int)$offset],
      ['$limit' => (int)$limit],
      ['$project' => ['_id' => '$_id']]
    ]
  ]);

// simple
$data = $query->execute();
// full documents
$query->fetchDocuments()->execute();
// for count
$count = $query->count()->execute();

Project information

Releases