Commit af72fa5f authored by Jeremy Mikola's avatar Jeremy Mikola

PHPLIB-59: Implement insertMany() and InsertManyResult

parent 55cf25d0
......@@ -422,6 +422,34 @@ class Collection
return new InsertOneResult($wr, $id);
}
/**
* Inserts the provided documents
*
* @see http://docs.mongodb.org/manual/reference/command/insert/
*
* @param array $documents The documents to insert
* @return InsertManyResult
*/
public function insertMany(array $documents)
{
$options = array_merge($this->getWriteOptions());
$bulk = new BulkWrite($options["ordered"]);
$insertedIds = array();
foreach ($documents as $i => $document) {
$insertedId = $bulk->insert($document);
if ($insertedId !== null) {
$insertedIds[$i] = $insertedId;
}
}
$writeResult = $this->manager->executeBulkWrite($this->ns, $bulk, $this->wc);
return new InsertManyResult($writeResult, $insertedIds);
}
/**
* Internal helper for delete one/many documents
* @internal
......
<?php
namespace MongoDB;
use MongoDB\Driver\WriteResult;
/**
* Result class for a multi-document write operation.
*/
class InsertManyResult
{
private $writeResult;
private $insertedIds;
/**
* Constructor.
*
* @param WriteResult $writeResult
* @param array $insertedIds
*/
public function __construct(WriteResult $writeResult, array $insertedIds = array())
{
$this->writeResult = $writeResult;
$this->insertedIds = $insertedIds;
}
/**
* Return the number of documents that were inserted.
*
* This value is undefined if the write was not acknowledged.
*
* @see InsertManyResult::isAcknowledged()
* @return integer
*/
public function getInsertedCount()
{
return $this->writeResult->getInsertedCount();
}
/**
* Return the map of inserted IDs that were generated by the driver.
*
* The index of each ID in the map corresponds to the document's position
* in bulk operation. If an inserted document already had an ID (e.g. it was
* generated by the application), it will not be present in this map.
*
* @return array
*/
public function getInsertedIds()
{
return $this->insertedIds;
}
/**
* Return whether this insert result was acknowledged by the server.
*
* If the insert was not acknowledged, other fields from the WriteResult
* (e.g. insertedCount) will be undefined.
*
* @return boolean
*/
public function isAcknowledged()
{
return $this->writeResult->isAcknowledged();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment