Symfony 2 comes with native support to connect to MongoDB using the DoctrineMongoDBBundle. Getting started is quite simple and there is not much that needs to be configured, but the manual does not say how to enable query logging although it is already possible with Symfony PR3.

Query logging allows you to debug queries sent to the DB server by writing them in a log file. It is very useful for cases when you issue a query and you are not getting the results you expected.

To log queries issued to MongoDB from your Symfony 2 application you need to set the logger_class parameter. Your config_dev.yml would look like:

    logger_class: Symfony\Bundle\DoctrineMongoDBBundle\Logger\DoctrineMongoDBLogger

Once this is added, you will see your queries logged in app/logs/dev.log or with the Profiler log viewer:

2010-10-01T15:30:47-04:00 INFO: { "findOne": "1", "query": { "_id": ObjectId("4c61c3c63c95282f0b0d0100"), "stype": { "$in": [ "server" ] } }, "fields": [ ], "class": "Application\ServerGrove\SGBundle\Document\Server", "db": "dev", "collection": "server" }

In the above example, we can see the following useful information. The output is in JSON notation:

  • findOne: defines the type of query. It will show if it was a find/findOne/update/delete query
  • query: the query sent to the server to filter the results
  • fields: list of fields to return, if array is empty, all will be returned.
  • class: the PHP class associated with the query
  • db: the database
  • collection: the collection