How to select specific fields with mongodb doctrine in symfony2

I want to find some documents from my mongo database, and I use the function: FindBy () unfortunately, this function has no field selection arguments, for example, the native mongodb driver with the function: find (). Does anyone know how to choose special fields in the Mondobb doctrine? Thanks

+6
source share
2 answers

You will need to use QueryBuilder with a select statement:

 $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

+5
source

findBy supports statements, but the supplied argument must be an array with the field name as the key for the first dimension, for example:

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

All Articles