Adds a document deletion operation with a filter to an ordered or unordered MongoDB bulk write.
The examples focus on the operation itself, including the important distinction: insert adds a document; delete queues deletion of matching documents.
What It Does
Use the examples below to observe the behavior on a small dataset before extending it to application code.
Syntax and Return Value
MongoDB\Driver\BulkWrite::delete(array|object $filter, ?array $deleteOptions = null): void
The displayed signature describes the API or identifies any correction or optional dependency. When a parameter is optional, supply it only when the task requires it.
Example 1: Basic Use
Requires PECL mongodb and a reachable server. BulkWrite queues changes locally; only executeBulkWrite sends them. Cursors stream server results.
<?php
$manager = new MongoDB\Driver\Manager(getenv("MONGODB_URI") ?: "mongodb://127.0.0.1:27017");
$bulk = new MongoDB\Driver\BulkWrite();
$bulk->delete(["task" => 'ship'], ["limit" => 1]);
$result = $manager->executeBulkWrite('demo.tasks', $bulk);
echo $result->isAcknowledged() ? "acknowledged" : "unacknowledged";
This example is meant for a configured application environment; output depends on the installed extension, external service, or database contents.
Example 2: Checking Behavior
Requires PECL mongodb and a reachable server. BulkWrite queues changes locally; only executeBulkWrite sends them. Cursors stream server results.
<?php
$manager = new MongoDB\Driver\Manager(getenv("MONGODB_URI") ?: "mongodb://127.0.0.1:27017");
$bulk = new MongoDB\Driver\BulkWrite();
$bulk->delete(["task" => 'review'], ["limit" => 1]);
$result = $manager->executeBulkWrite('demo.audit', $bulk);
echo $result->isAcknowledged() ? "acknowledged" : "unacknowledged";
This example is meant for a configured application environment; output depends on the installed extension, external service, or database contents.
Example 3: Practical Example
Requires PECL mongodb and a reachable server. BulkWrite queues changes locally; only executeBulkWrite sends them. Cursors stream server results.
<?php
$manager = new MongoDB\Driver\Manager(getenv("MONGODB_URI") ?: "mongodb://127.0.0.1:27017");
$bulk = new MongoDB\Driver\BulkWrite();
$bulk->delete(["task" => 'archive'], ["limit" => 1]);
$result = $manager->executeBulkWrite('demo.queue', $bulk);
echo $result->isAcknowledged() ? "acknowledged" : "unacknowledged";
This example is meant for a configured application environment; output depends on the installed extension, external service, or database contents.
Important Behavior and Edge Cases
Delete filters must be intentional; use limit 1 for a single match and 0 for all matches.
This API is supplied by an optional extension that is not installed in the local test runtime, or requires a different PHP version. The example must be verified in a matching environment.
Comparison with Related Approaches
| Approach | Difference |
|---|---|
| MongoDB\Driver\BulkWrite::delete | Adds a document deletion operation with a filter to an ordered or unordered MongoDB bulk write. |
| Related API or alternative | insert adds a document; delete queues deletion of matching documents. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Ignoring the operation-specific limits | Delete filters must be intentional; use limit 1 for a single match and 0 for all matches. |
| Confusing it with a related API | insert adds a document; delete queues deletion of matching documents. |
Key Takeaways
- Adds a document deletion operation with a filter to an ordered or unordered MongoDB bulk write.
- Delete filters must be intentional; use limit 1 for a single match and 0 for all matches.
- insert adds a document; delete queues deletion of matching documents.
Use MongoDB\Driver\BulkWrite::delete when its documented behavior matches the problem. Test the relevant edge cases in your own runtime before deploying it.