bulk write mongodb php 1

bulk write mongodb php

 'one'];
$document2 = ['_id' => 'custom ID', 'title' => 'two'];
$document3 = ['_id' => new MongoDB\BSON\ObjectId, 'title' => 'three'];

$_id1 = $bulk->insert($document1);
$_id2 = $bulk->insert($document2);
$_id3 = $bulk->insert($document3);

var_dump($_id1, $_id2, $_id3);

$manager = new MongoDB\Driver\Manager('mongodb://localhost:27017');
$result = $manager->executeBulkWrite('db.collection', $bulk);

?>

Here is what the above code is Doing:
1. Create a new BulkWrite object.
2. Create three documents.
3. Insert the documents into the BulkWrite object.
4. Create a Manager object.
5. Execute the bulk write.

The BulkWrite object is used to store the write operations. The insert() method is used to add documents to the BulkWrite object. The executeBulkWrite() method is used to execute the bulk write.

The insert() method returns the _id of the inserted document. The _id is generated by the driver unless a custom _id is specified.

The executeBulkWrite() method returns a WriteResult object. The WriteResult object contains information about the write operation.

Similar Posts