Installation

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

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:

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

Install the bundle with Composer

To install DoctrineMongoDBBundle with Composer run the following command:

$ 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// config/bundles.php return [ // ... Doctrine\Bundle\MongoDBBundle\DoctrineMongoDBBundle::class => ['all' => true], ];
2
3
4
5

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

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

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# config/services.yaml parameters: mongodb_server: "mongodb://username:password@localhost:27017/?authSource=auth-db"
2
3

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