'PHP, Mongodb, BulkWrite

I need to insert records into the mongodb database. I read the documentation and made a query using BulkWrite. But it inserts only 1 record and ends with an error:

Fatal error: Uncaught MongoDB\Driver\Exception\InvalidArgumentException: BulkWrite objects may only be executed once and this instance has already been executed

PHP script:

$bulk = new MongoDB\Driver\BulkWrite;

$manager = new MongoDB\Driver\Manager('mongodb://user:pass@ip:27017');

$lines = file('name.txt');

$document = array();

foreach ($lines as $row) {

        $document[] = array( 
            "name" => $row, 
            "description" => "all", 
        );

        $insert = $bulk->insert($document);

        $result = $manager->executeBulkWrite('test.collection', $bulk);
}

Why is it executed only once and not until the file runs out?

How do I make the loop work correctly?

Thank you!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source