Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove multiple documents in Doctrine MongoDB ODM

How can I remove multiple documents in doctrine mongodb odm? In PHP class I should be able to do it using the following code:

 $db->collection->remove(array("age" => 18));

How can I do it in doctrine mongo odm?

like image 919
dextervip Avatar asked Jul 02 '12 20:07

dextervip


1 Answers

You have two options. Using the DocumentManager, you can request the collection for a given class. This will actually return a Doctrine\MongoDB\Collection instance, which wraps the base MongoCollection to support logging and events. The underlying MongoCollection is available if you really want it, too:

$collection = $dm->getDocumentCollection('Documents\User');
$collection->remove(array('age' => 18));

// Use the raw MongoCollection to bypass logging and events
$mongoCollection = $collection->getMongoCollection();
$mongoCollection->remove(array('age' => 18));

Alternatively, you can use the query builder API:

$qb = $dm->createQueryBuilder('Documents\User');
$qb->remove()
    ->field('age')->equals(18)
    ->getQuery()
    ->execute();

If you stop at getQuery(), you can use the debug() method on the returned query object to see what Doctrine will execute against the database.

like image 58
jmikola Avatar answered Sep 20 '22 08:09

jmikola