WARNING:

You are browsing the documentation for version 4.0 which is not maintained anymore.

If some of your projects are still using this version, consider upgrading.

Installation

4.0 version

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 through a Contrib Recipe therefore you need to allow its usage:

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 just run the following command:

1
composer require doctrine/mongodb-odm-bundle

All that is left to do is to update your AppKernel.php file, and register the new bundle:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
// app/AppKernel.php
public function registerBundles()
{
    $bundles = array(
        // ...
        new Doctrine\Bundle\MongoDBBundle\DoctrineMongoDBBundle(),
    );

    // ...
}

Configuration

To get started, you’ll need some basic configuration that sets up the document manager. The easiest way is to enable auto_mapping, which will activate the MongoDB ODM across your application:

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

Note

Of course, you’ll also need to make sure that the MongoDB server is running in the background. For more details, see the MongoDB Quick Start guide.

Authentication

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

# app/config/parameters.yaml parameters:

mongodb_server: “mongodb://username:password@localhost:27017/?authSource=auth-db”

Note

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

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