Installation

4.1 version
Maintained

Installation

This chapter assumes you have Composer installed globally, as explained in the installation chapter of the Composer documentation.

Note

The ODM requires the MongoDB driver (mongodb).

Install the bundle with Symfony Flex

A Flex recipe for the DoctrineMongoDBBundle is provided as a Contrib Recipe. You need to allow its usage first:

1
composer config extra.symfony.allow-contrib true
1
composer require doctrine/mongodb-odm-bundle

Install the bundle with Composer

To install DoctrineMongoDBBundle with Composer run the following command:

1
composer require doctrine/mongodb-odm-bundle

Enable the Bundle

Your bundle should be automatically enabled if you use Flex. Otherwise, you'll need to manually enable the bundle by adding the following line in the config/bundles.php file of your project:

1
2
3
4
5
6
7
// config/bundles.php
<?php

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

Configuration

Flex recipe will automatically create the config/packages/doctrine_mongodb.yaml file with default configuration. Without Flex you need to create the file manually and fill it with some basic configuration that sets up the document manager. The recommended way is to enable auto_mapping, which will activate the MongoDB ODM across your application:

1
2
3
# config/services.yaml
parameters:
    mongodb_server: "mongodb://localhost:27017"
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# config/packages/doctrine_mongodb.yaml
doctrine_mongodb:
    connections:
        default:
            server: "%mongodb_server%"
            options: {}
    default_database: test_database
    document_managers:
        default:
            auto_mapping: true

Note

Please also make sure that the MongoDB server is running in the background. For more details, see the MongoDB Installation Tutorials.

Tip

You can configure bundle options that depend on where your application is run (e.g. during tests or development) with Environment Variables.

Authentication

If you use authentication on your MongoDB database, then you can provide username, password, and authentication database in the following way:

1
2
3
# config/services.yaml
parameters:
    mongodb_server: "mongodb://username:[email protected]:27017/?authSource=auth-db"

Note

The authentication database is different from the default database used by MongoDB.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.