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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With