如何通过 DoctrineMongoDBBundle 截断 MongoDB 集合?

How to truncate a MongoDB collection via the DoctrineMongoDBBundle?

我有一个 mongoDB 集合,我想以编程方式截断它(删除该集合中的所有文档)。我这样做了:

    $collection = $this
        ->container
        ->get('doctrine_mongodb')
        ->getRepository('AppBundle:User');

    $document_manager = $this
        ->container
        ->get('doctrine_mongodb')
        ->getManager();

    if($override){
        $document_manager->remove($collection);

其中 User 是集合名称。但它不起作用。如何正确地从集合中删除所有文档?

首先,得到一个collection:

$collection = $document_manager->getDocumentCollection('AppBundle:User'); // or just a class name

remove来自collection的所有文档传递空数组以匹配所有文档:

$collection->remove([]);

drop collection:

$collection->drop();