Search code examples
phpmongodbdoctrineodm

Doctrine-ODM (MongoDB) - FindByMultipleIDs


I am using Doctrine ODM(MongoDB). I am trying to write doctrine odm query builder to get the data where IDs IN (1,2,3). But i am not able to get it. Please help me on this.

I want to create odm query builder for the normal sql query like below,

SELECT * FROM USER WHERE id IN (1,2,3)

I hope there is no default function like findByID()


Solution

  • From the reference :

     $queryBuilder = $dm->createQueryBuilder('User')->field('id')->in([$id1,$id2,$id3]);
     $usersCollection = $queryBuilder->getQuery()->execute();
    

    or for a single document:

    $user = $dm->getRepository('User')->find($id);