简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM