Sokil\Mongo\Collection::batchInsert PHP Method

batchInsert() public method

Prior to version 1.5.0 of the driver it was possible to use MongoCollection::batchInsert(), however, as of 1.5.0 that method is now discouraged. You can use Collection::createBatchInsert()
public batchInsert ( array $rows, $validate = true ) : Collection
$rows array list of documents to insert, defined as arrays
return Collection
    public function batchInsert($rows, $validate = true)
    {
        if ($validate) {
            $document = $this->createDocument();
            foreach ($rows as $row) {
                $document->merge($row);
                if (!$document->isValid()) {
                    throw new InvalidDocumentException('Document is invalid on batch insert');
                }
                $document->reset();
            }
        }
        $result = $this->getMongoCollection()->batchInsert($rows);
        // If the w parameter is set to acknowledge the write,
        // returns an associative array with the status of the inserts ("ok")
        // and any error that may have occurred ("err").
        if (is_array($result)) {
            if ($result['ok'] != 1) {
                throw new Exception('Batch insert error: ' . $result['err']);
            }
            return $this;
        }
        // Otherwise, returns TRUE if the batch insert was successfully sent,
        // FALSE otherwise.
        if (!$result) {
            throw new Exception('Batch insert error');
        }
        return $this;
    }

Usage Example

コード例 #1
0
ファイル: BatchTest.php プロジェクト: agolomazov/php-mongo
 public function testDelete()
 {
     // insert
     $this->collection->batchInsert(array(array('a' => 1), array('a' => 2), array('a' => 3), array('a' => 4), array('a' => 5), array('a' => 6)));
     // delete
     $batch = new BatchDelete($this->collection);
     $batch->delete(array('a' => 2))->delete($this->collection->expression()->where('a', 4))->delete(function (Expression $e) {
         $e->where('a', 6);
     })->execute();
     // test
     $result = $this->collection->findAsArray()->sort(array('a' => 1))->map(function ($data) {
         unset($data['_id']);
         return $data;
     });
     $this->assertEquals(array_values($result), array(array('a' => 1), array('a' => 3), array('a' => 5)));
 }
All Usage Examples Of Sokil\Mongo\Collection::batchInsert