How to select certain fields with mongodb doctrine in symfony2 How to select certain fields with mongodb doctrine in symfony2 symfony symfony

How to select certain fields with mongodb doctrine in symfony2


You will need to use QueryBuilder with the select operator:

$result = $dm->createQueryBuilder('User')->select('field1', 'field2')->field('field3')->equals('somevalue')->getQuery()->execute();

http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/reference/query-builder-api.html


findBy does support operators, but the argument provided should be an array with the field name as key for the first dimension, like so :

$results = $yourRepositoryInstance->findBy(['somefield' => ['$in' => $arrayOfValues]]);


If you need to dynamically want to select fields, use the field name as an array.

$selects = ['field1','field2','field3'];  // you can pass through api or else$result = $dm->createQueryBuilder('Collection')->select($selects)->getQuery()->execute();