简体   繁体   English

如何在Doctrine MongoDB ODM中删除多个文档

[英]How to remove multiple documents in Doctrine MongoDB ODM

How can I remove multiple documents in doctrine mongodb odm? 如何删除mongodb odm原则中的多个文档? In PHP class I should be able to do it using the following code: 在PHP类中,我应该可以使用以下代码进行操作:

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

How can I do it in doctrine mongo odm? 我该如何在mongo odm学说中做到这一点?

You have two options. 您有两个选择。 Using the DocumentManager, you can request the collection for a given class. 使用DocumentManager,可以请求给定类的集合。 This will actually return a Doctrine\\MongoDB\\Collection instance, which wraps the base MongoCollection to support logging and events. 实际上,这将返回Doctrine\\MongoDB\\Collection实例,该实例包装基本的MongoCollection以支持日志记录和事件。 The underlying MongoCollection is available if you really want it, too: 如果确实需要基础MongoCollection也可以使用它:

$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 : 或者,您可以使用查询构建器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. 如果在getQuery()处停止,则可以在返回的查询对象上使用debug()方法,以查看将对数据库执行的Doctrine。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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