Queues a BSON document insertion in a MongoDB BulkWrite batch.
The examples focus on the operation itself, including the important distinction: Manager::executeBulkWrite sends the queued operations to the server.
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::insert(array|object $document): mixed
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->insert(["task" => 'ship', "priority" => 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->insert(["task" => 'review', "priority" => 2]);
$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->insert(["task" => 'archive', "priority" => 3]);
$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
It queues work until Manager::executeBulkWrite; duplicate keys can raise errors at execution.
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::insert | Queues a BSON document insertion in a MongoDB BulkWrite batch. |
| Related API or alternative | Manager::executeBulkWrite sends the queued operations to the server. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Ignoring the operation-specific limits | It queues work until Manager::executeBulkWrite; duplicate keys can raise errors at execution. |
| Confusing it with a related API | Manager::executeBulkWrite sends the queued operations to the server. |
Key Takeaways
- Queues a BSON document insertion in a MongoDB BulkWrite batch.
- It queues work until Manager::executeBulkWrite; duplicate keys can raise errors at execution.
- Manager::executeBulkWrite sends the queued operations to the server.
Use MongoDB\Driver\BulkWrite::insert when its documented behavior matches the problem. Test the relevant edge cases in your own runtime before deploying it.