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?
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();
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