The database classicaly used with Symfony 4 is MySQL / MariaDB throught Doctrine project. However, some projects need a NoSQL database like MongoDB in replacement of the relational database. It's not a reason to not develop them with Symfony 4 !

The DoctrineMongoDBBundle is a project that allows to use MongoDB since Symfony 2. But the documentation is not very clear for an utilisation with Symfony 4.

Symfony needs PHP 7 but DoctrineMongoDBBundle is not actually compatible. This package needs ext-mongo PHP extension but it is deprecated to ext-mongodb PHP extension. To create the compatibility you need install the following package before :

composer require alcaeus/mongo-php-adapter

Now, you can install the bundle :

composer require doctrine/mongodb-odm-bundle

After installation, you must configure it. Create or edit config/packages/doctrine.yaml to add a code like this:

doctrine_mongodb:
    connections:
        default:
            server: "%mongodb_server%"
            options: {}
    default_database: yourdbnamehere
    document_managers:
        default:
            auto_mapping: true
            mappings:
                App:
                    is_bundle: false
                    type: annotation
                    dir: '%kernel.project_dir%/src/Document'
                    prefix: 'App\Document'
                    alias: App

Since we will using Mongo Document, you must create a folder with path:

src/Document

At this step we need define the mongodb_server variable (See Yaml above). Edit the services.yaml file and add the following declaration:

parameters:
  mongodb_server: "mongodb://localhost:27017"

localhost can be replace by the mongo container name if you use Docker

If composer did not make any change in bundles.php, you must add this declaration:

<?php

return [
    [...]
    Doctrine\Bundle\MongoDBBundle\DoctrineMongoDBBundle::class => ['all' => true],
    [...]
];

Now you can control your Mongocontainer with this command:

bin/console debug:container

The installation is now over, you can create your first Mongo Document. In src/Document folder, create a document class file User.php like:

<?php

namespace App\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
/**
 * @MongoDB\Document
 */
class User
{
    /**
     * @MongoDB\Id
     */
    protected $id;

    /**
     * @MongoDB\Field(type="string")
     */
    protected $firstname;

    /**
     * @MongoDB\Field(type="string")
     */
    protected $lastname;

    /**
     * @MongoDB\Field(type="string")
     */
    protected $email;

    /**
     * @MongoDB\Field(type="string")
     */
    protected $password;

    /**
     * @MongoDB\Field(type="date")
     */
    protected $creation_date;

Don't forget to create all setter and gettter in the class

Now in your controller you can use it like this:

<?php

namespace App\Controller;

use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\JsonResponse;
use App\Document\User;
use Doctrine\ODM\MongoDB\DocumentManager as DocumentManager;

class MongoController
{
    /**
     * @Route("/mongoTest", methods={"GET"})
     */
    public function mongoTest(DocumentManager $dm)
    {
        $user = new User();
        $user->setEmail("vincent@vfac.fr");
        $user->setFirstname("Vincent");
        $user->setLastname("Vincent");
        $user->setPassword(md5("VFACP@ssw0rd"));

        $dm->persist($user);
        $dm->flush();
        return new JsonResponse(array('Status' => 'OK'));
    }
}

Previous Post