Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to truncate a MongoDB collection via the DoctrineMongoDBBundle?

I have a mongoDB collection and I want to programatically truncate it (remove all documents in this collection). I did this:

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

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

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

Where User is the name of collection. But it does not work. How to properly remove all documents from a collection?

like image 650
msniezko Avatar asked Dec 15 '25 00:12

msniezko


1 Answers

First, get a collection:

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

To remove all the documents from the collection pass empty array to match all documents:

$collection->remove([]);

To drop the collection:

$collection->drop();
like image 192
Alex Blex Avatar answered Dec 16 '25 22:12

Alex Blex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!