PHP & MongoDB - Deleting Documents



Steps to Delete a Document in a collection

First step to do any operation is to create a MongoDB Client instance.

$uri = "mongodb://localhost:27017";
// connect to mongodb
$client = new MongoDB\Client($uri);

Second step is to prepare and execute a filter to select a document and delete document using deleteOne() command.

// select a collection of a database
$collection = $client->selectCollection('myDb', 'sampleCollection');

// Define a Filter
$filter = ['e_mail' => 'mahesh_parashar@gmail.com'];

// delete the document
$deletedResult = $collection->deleteOne($filter);

Example - Deleting a Document of a MongoDB Collection

Try the following example to delete a single document of a MongoDB collection −

index.php

<?php
   require __DIR__ . '\vendor\autoload.php';
   $uri = "mongodb://localhost:27017";

   // connect to mongodb
   $client = new MongoDB\Client($uri);

   // select a collection of a database
   $collection = $client->selectCollection('myDb', 'sampleCollection');

   // Define a Filter
   $filter = ['e_mail' => 'mahesh_parashar@gmail.com'];

   // delete the document
   $deletedResult = $collection->deleteOne($filter);

   printf("Deleted %d document.", $deletedResult->getDeletedCount());
?>

Output

Access the index.php deployed on apache web server and verify the output.

Deleted 1 document.

Steps to Delete Multiple Documents from a collection

Prepare and execute a command to delete documents using deleteMany() command.

// select a collection of a database
$collection = $client->selectCollection('myDb', 'sampleCollection');

// Define a Filter
$filter = ['phone' => '90000123451'];

// delete the documents
$deletedResult = $collection->deleteMany($filter);

Example - Deleting Multiple Documents of a MongoDB Collection

Try the following example to delete multiple documents of a MongoDB collection −

index.php

<?php
   require __DIR__ . '\vendor\autoload.php';
   $uri = "mongodb://localhost:27017";

   // connect to mongodb
   $client = new MongoDB\Client($uri);

   // select a collection of a database
   $collection = $client->selectCollection('myDb', 'sampleCollection');

   // Define a Filter
   $filter = ['phone' => '90000123451'];

   // delete the documents
   $deletedResult = $collection->deleteMany($filter);

   printf("Deleted %d document(s).", $deletedResult->getDeletedCount());
?>

Output

Access the index.php deployed on apache web server and verify the output.

Deleted 2 document(s).

Steps to Delete All Documents from a collection

Prepare and execute a command to delete all documents using deleteMany() command.

// select a collection of a database
$collection = $client->selectCollection('myDb', 'sampleCollection');

// Define an empty Filter to match all documents
$filter = [];

// delete all the documents
$deletedResult = $collection->deleteMany($filter);

Example - Deleting All Documents of a MongoDB Collection

Try the following example to delete all documents of a MongoDB collection −

index.php

<?php
   require __DIR__ . '\vendor\autoload.php';
   $uri = "mongodb://localhost:27017";

   // connect to mongodb
   $client = new MongoDB\Client($uri);

   // select a collection of a database
   $collection = $client->selectCollection('myDb', 'sampleCollection');

   // Define a Filter
   $filter = [];

   // delete all the documents
   $deletedResult = $collection->deleteMany($filter);

   printf("Deleted %d document(s).", $deletedResult->getDeletedCount());
?>

Output

Access the index.php deployed on apache web server and verify the output.

Deleted 2 document(s).
Advertisements