This example is covered in the Multiple Connections section of the bundle's config reference. Each connection and document manager should have a unique name, and then the document manager configuration should refer to the appropriate connection name. I believe the last bit is what was missing from your original example. Quoting the bundle docs:
doctrine_mongodb:
default_database: hello_%kernel.environment%
default_connection: conn2
default_document_manager: dm2
metadata_cache_driver: apc
connections:
conn1:
server: mongodb://a.example.com:27017
conn2:
server: mongodb://b.example.com:27017
document_managers:
dm1:
connection: conn1
metadata_cache_driver: xcache
mappings:
AcmeDemoBundle: ~
dm2:
connection: conn2
mappings:
AcmeHelloBundle: ~
I would suggest using names other than "default" for your connections and document managers, to avoid confusion. Take note that the document manager names will now be part of the service ID. Per the above configuration, the second connection and document manager will be aliased as the default service IDs (see: default_connection
and default_document_manager
). Quoting a second example from the documentation, the specific service IDs will be:
$conn1 = $container->get('doctrine_mongodb.odm.conn1_connection');
$conn2 = $container->get('doctrine_mongodb.odm.conn2_connection');
$dm1 = $container->get('doctrine_mongodb.odm.dm1_document_manager');
$dm2 = $container->get('doctrine_mongodb.odm.dm2_document_manager');