batchInsert() public méthode

Inserts multiple documents into this collection
public batchInsert ( array &$a, array $options = [] ) : mixed
$a array An array of arrays.
$options array Options for the inserts.
Résultat mixed If "safe" is set, returns an associative array with the status of the inserts ("ok") and any error that may have occured ("err"). Otherwise, returns TRUE if the batch insert was successfully sent, FALSE otherwise.
Exemple #1
0
 /**
  * Executes all queued document insertions.
  *
  * Queued documents without an ID will inserted in a batch and queued
  * documents with an ID will be upserted individually.
  *
  * If no inserts are queued, invoking this method is a NOOP.
  *
  * @param array $options Options for batchInsert() and update() driver methods
  */
 public function executeInserts(array $options = array())
 {
     if (!$this->queuedInserts) {
         return;
     }
     $inserts = array();
     foreach ($this->queuedInserts as $oid => $document) {
         $data = $this->pb->prepareInsertData($document);
         // Set the initial version for each insert
         if ($this->class->isVersioned) {
             $versionMapping = $this->class->fieldMappings[$this->class->versionField];
             if ($versionMapping['type'] === 'int') {
                 $nextVersion = $this->class->reflFields[$this->class->versionField]->getValue($document);
                 $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion);
             } elseif ($versionMapping['type'] === 'date') {
                 $nextVersionDateTime = new \DateTime();
                 $nextVersion = new \MongoDate($nextVersionDateTime->getTimestamp());
                 $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersionDateTime);
             }
             $data[$versionMapping['name']] = $nextVersion;
         }
         $inserts[$oid] = $data;
     }
     if ($inserts) {
         try {
             $this->collection->batchInsert($inserts, $options);
         } catch (\MongoException $e) {
             $this->queuedInserts = array();
             throw $e;
         }
     }
     $this->queuedInserts = array();
 }
Exemple #2
0
 /**
  * Execute the batchInsert query.
  *
  * @see Collection::batchInsert()
  * @param array $a
  * @param array $options
  * @return array|boolean
  */
 protected function doBatchInsert(array &$a, array $options = array())
 {
     $options = isset($options['safe']) ? $this->convertWriteConcern($options) : $options;
     $options = isset($options['wtimeout']) ? $this->convertWriteTimeout($options) : $options;
     $options = isset($options['timeout']) ? $this->convertSocketTimeout($options) : $options;
     return $this->mongoCollection->batchInsert($a, $options);
 }
Exemple #3
0
 /**
  * Insert multiple documents defined as arrays
  *
  * 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()
  *
  * @param array $rows list of documents to insert, defined as arrays
  * @return \Sokil\Mongo\Collection
  * @throws \Sokil\Mongo\Document\InvalidDocumentException
  * @throws \Sokil\Mongo\Exception
  */
 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->_mongoCollection->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;
 }
 /**
  * batchInsert.
  */
 public function batchInsert(array $a, array $options = array())
 {
     $this->time->start();
     $return = parent::batchInsert($a, $options);
     $time = $this->time->stop();
     $this->log(array('type' => 'batchInsert', 'nb' => count($a), 'data' => $a, 'options' => $options, 'time' => $time));
     return $return;
 }
Exemple #5
0
 /**
  * {@inheritDoc}
  */
 public function batchPersist(array $batch)
 {
     $insert = [];
     foreach ($batch as $idx => $doc) {
         $struc = $this->factory->desegregate($doc);
         if (array_key_exists('id', $struc)) {
             unset($struc['id']);
         }
         if (!is_null($doc->getId())) {
             $struc['_id'] = $doc->getId();
         }
         $insert[$idx] = $struc;
     }
     $this->collection->batchInsert($insert);
     foreach ($batch as $idx => $doc) {
         $doc->setId($insert[$idx]['_id']);
     }
 }
 public function testSafeRemove2()
 {
     $result = $this->object->remove(array(), array("safe" => true));
     $this->assertEquals(true, (bool) $result['ok']);
     $this->assertEquals(0, $result['n']);
     $this->assertNull($result['err']);
     $this->object->batchInsert(array(array("x" => 1), array("x" => 1), array("x" => 1)));
     $result = $this->object->remove(array(), array("safe" => true));
     $this->assertEquals(true, (bool) $result['ok']);
     $this->assertEquals(3, $result['n']);
     $this->assertNull($result['err']);
 }
function mongodb_fixture()
{
    $db_name = DB_NAME;
    $host_path = "mongodb://" . HOST . ":" . PORT;
    $mongodb = new MongoDB(new Mongo($host_path), $db_name);
    $collection_name = COLLECTION_NAME;
    $collection = new MongoCollection($mongodb, $collection_name);
    $collection->remove(array());
    //remove all documents of that collection
    $fixture = array(array('_id' => 1, 'tags' => array('dog', 'cat')), array('_id' => 2, 'tags' => array('cat')), array('_id' => 3, 'tags' => array('mouse', 'cat', 'dog')), array('_id' => 4, 'tags' => array()));
    $collection->batchInsert($fixture, array('safe' => true));
    return $mongodb;
}
 public function testBatchInsert()
 {
     $this->assertFalse($this->object->batchInsert(array()));
     $this->assertFalse($this->object->batchInsert(array(1, 2, 3)));
     $this->assertTrue($this->object->batchInsert(array('z' => array('foo' => 'bar'))));
     $a = array(array("x" => "y"), array("x" => "z"), array("x" => "foo"));
     $this->object->batchInsert($a);
     $this->assertEquals(4, $this->object->count());
     $cursor = $this->object->find()->sort(array("x" => -1));
     $x = $cursor->getNext();
     $this->assertEquals('bar', $x['foo']);
     $x = $cursor->getNext();
     $this->assertEquals('z', $x['x']);
     $x = $cursor->getNext();
     $this->assertEquals('y', $x['x']);
     $x = $cursor->getNext();
     $this->assertEquals('foo', $x['x']);
 }
Exemple #9
0
 /**
  * Insert a new record into the database.
  *
  * @param  array  $values
  * @return bool
  */
 public function insert(array $values)
 {
     // Since every insert gets treated like a batch insert, we will have to detect
     // if the user is inserting a single document or an array of documents.
     $batch = true;
     foreach ($values as $value) {
         // As soon as we find a value that is not an array we assume the user is
         // inserting a single document.
         if (!is_array($value)) {
             $batch = false;
             break;
         }
     }
     if (!$batch) {
         $values = array($values);
     }
     // Batch insert
     $result = $this->collection->batchInsert($values);
     return 1 == (int) $result['ok'];
 }
 /**
  * Executes all queued document insertions.
  *
  * Queued documents without an ID will inserted in a batch and queued
  * documents with an ID will be upserted individually.
  *
  * If no inserts are queued, invoking this method is a NOOP.
  *
  * @param array $options Options for batchInsert() and update() driver methods
  */
 public function executeInserts(array $options = array())
 {
     if (!$this->queuedInserts) {
         return;
     }
     $inserts = array();
     $options = $this->getWriteOptions($options);
     foreach ($this->queuedInserts as $oid => $document) {
         $data = $this->pb->prepareInsertData($document);
         // Set the initial version for each insert
         if ($this->class->isVersioned) {
             $versionMapping = $this->class->fieldMappings[$this->class->versionField];
             if ($versionMapping['type'] === 'int') {
                 $nextVersion = max(1, (int) $this->class->reflFields[$this->class->versionField]->getValue($document));
                 $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion);
             } elseif ($versionMapping['type'] === 'date') {
                 $nextVersionDateTime = new \DateTime();
                 $nextVersion = new \MongoDate($nextVersionDateTime->getTimestamp());
                 $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersionDateTime);
             }
             $data[$versionMapping['name']] = $nextVersion;
         }
         $inserts[$oid] = $data;
     }
     if ($inserts) {
         try {
             $this->collection->batchInsert($inserts, $options);
         } catch (\MongoException $e) {
             $this->queuedInserts = array();
             throw $e;
         }
     }
     /* All collections except for ones using addToSet have already been
      * saved. We have left these to be handled separately to avoid checking
      * collection for uniqueness on PHP side.
      */
     foreach ($this->queuedInserts as $document) {
         $this->handleCollections($document, $options);
     }
     $this->queuedInserts = array();
 }
 /**
  * Insert a new record into the database.
  *
  * @param  array  $values
  * @return bool
  */
 public function insert(array $values)
 {
     $start = microtime(true);
     // Since every insert gets treated like a batch insert, we will have to detect
     // if the user is inserting a single document or an array of documents.
     $batch = true;
     foreach ($values as $value) {
         // As soon as we find a value that is not an array we assume the user is
         // inserting a single document.
         if (!is_array($value)) {
             $batch = false;
             break;
         }
     }
     if (!$batch) {
         $values = array($values);
     }
     // Batch insert
     $result = $this->collection->batchInsert($values);
     // Log query
     $this->connection->logQuery($this->from . '.batchInsert(' . json_encode($values) . ')', array(), $this->connection->getElapsedTime($start));
     return 1 == (int) $result['ok'];
 }
 /**
  * 批量插入一组新的数据
  *
  * @param array $array 每一个元素包含一个要插入的行
  * @return boolean
  */
 function batchInsert(array $array)
 {
     return $this->_collection->batchInsert($array);
 }
Exemple #13
0
 /**
  * Inserts multiple documents into this collection
  * @link http://php.net/manual/en/mongocollection.batchinsert.php
  * 
  * @param array $docs
  * @param array $options
  * @return mixed
  */
 public function batchInsert(array &$docs, array $options = [])
 {
     $a = [];
     foreach ($docs as $doc) {
         $a[] = $this->db->toMongoType($doc);
     }
     $ret = parent::batchInsert($a, $options);
     foreach ($a as $i => $values) {
         $this->setDocId($docs[$i], $values);
     }
     return $ret;
 }
Exemple #14
0
 /**
  * Compiles an array of exec query and runs it.
  *
  * @param   array of queries  sql
  * @param   string  Collection name
  * @return  Database_Result  Query result
  */
 public function multi_exec($sql, $collection = '')
 {
     if (empty($sql) || !is_array($sql)) {
         return false;
     }
     // No link? Connect!
     $this->link or $this->connect();
     try {
         $insert = array();
         foreach ($sql as $q) {
             if ($q['action'] == 'insert') {
                 // collect inserts
                 $insert[] = $q['data'];
             } else {
                 // update or delete
                 $result = $this->single_exec($q, $collection);
                 self::$queries++;
             }
         }
         if (!empty($insert)) {
             $this->latest_query = $insert;
             // Collection
             $col = new MongoCollection($this->link, $collection);
             $res = $col->batchInsert($insert);
             $result = array(0, 1);
             self::$queries += sizeof($insert);
         }
     } catch (Exception $e) {
         if (DEBUG) {
             $this->print_error($sql, $res);
             die;
         }
         $result = array(0, 0);
     }
     return $result;
 }
Exemple #15
0
 /**
  * 批量插入数据
  *
  * @see MongoCollection::batchInsert()
  */
 public function batchInsert(array $documents, array $options = NULL)
 {
     array_walk($documents, function (&$row, $key) {
         $row['__CREATE_TIME__'] = $row['__MODIFY_TIME__'] = new \MongoDate();
         $row['__REMOVED__'] = false;
     });
     return parent::batchInsert($documents, $options);
 }
Exemple #16
0
 protected function _call($command, array $arguments = array(), array $values = NULL)
 {
     $start = microtime(true);
     $this->_connected or $this->connect();
     extract($arguments);
     if (isset($collection_name)) {
         $c = new \MongoCollection($this->_db, $collection_name);
     }
     switch ($command) {
         case 'ensure_index':
             $r = $c->ensureIndex($keys, $options);
             break;
         case 'create_collection':
             $r = $this->_db->createCollection($name, $capped, $size, $max);
             break;
         case 'drop_collection':
             $r = $this->_db->dropCollection($name);
             break;
         case 'command':
             $r = $this->_db->command($values);
             break;
         case 'execute':
             $r = $this->_db->execute($code, $args);
             break;
         case 'batch_insert':
             $r = $c->batchInsert($values);
             break;
         case 'count':
             $r = $c->count($query);
             break;
         case 'find_one':
             $r = $c->findOne($query, $fields);
             break;
         case 'find':
             $r = $c->find($query, $fields);
             break;
         case 'group':
             $r = $c->group($keys, $initial, $reduce, $condition);
             break;
         case 'update':
             $r = $c->update($criteria, $values, $options);
             break;
         case 'insert':
             $r = $c->insert($values, $options);
             return $values;
             break;
         case 'remove':
             $r = $c->remove($criteria, $options);
             break;
         case 'save':
             $r = $c->save($values, $options);
             break;
         case 'get_file':
             $r = $this->gridFS()->findOne($criteria);
             break;
         case 'get_files':
             $r = $this->gridFS()->find($query, $fields);
             break;
         case 'set_file_bytes':
             $r = $this->gridFS()->storeBytes($bytes, $extra, $options);
             break;
         case 'set_file':
             $r = $this->gridFS()->storeFile($filename, $extra, $options);
             break;
         case 'remove_file':
             $r = $this->gridFS()->remove($criteria, $options);
             break;
     }
     $this->log($command, $start, $arguments);
     return $r;
 }
Exemple #17
0
/**
 * Demonstrated logging a collection.batchInsert().
 *
 * @param \MongoCollection $collection
 *   The demo collection.
 * @param int $max
 *   The maximum number of documents to insert in a loop.
 * @param \Psr\Log\LoggerInterface $logger
 *   The logger instance.
 */
function batch_insert_demo(MongoCollection $collection, $max, LoggerInterface $logger)
{
    $logger->debug("Batch-inserting {$max} documents");
    $docs = prepare_documents($max);
    $logger instanceof TimingLoggerInterface && $logger->startLap();
    $collection->batchInsert($docs);
    $logger->debug('');
}
Exemple #18
0
 protected function doBatchInsert(array &$a, array $options = array())
 {
     return $this->mongoCollection->batchInsert($a, $options);
 }
Exemple #19
0
 /**
  * {@inheritdoc}
  */
 protected function commitTransaction($extras = null)
 {
     if (empty($this->batchRecords) && empty($this->batchIds)) {
         return null;
     }
     $updates = ArrayUtils::get($extras, 'updates');
     $ssFilters = ArrayUtils::get($extras, 'ss_filters');
     $fields = ArrayUtils::get($extras, ApiOptions::FIELDS);
     $requireMore = ArrayUtils::get($extras, 'require_more');
     $out = [];
     switch ($this->getAction()) {
         case Verbs::POST:
             $result = $this->collection->batchInsert($this->batchRecords, ['continueOnError' => false]);
             static::processResult($result);
             $out = static::cleanRecords($this->batchRecords, $fields, static::DEFAULT_ID_FIELD);
             break;
         case Verbs::PUT:
             if (empty($updates)) {
                 throw new BadRequestException('Batch operation not supported for update by records.');
             }
             $filter = [static::DEFAULT_ID_FIELD => ['$in' => $this->batchIds]];
             $criteria = static::buildCriteriaArray($filter, null, $ssFilters);
             $result = $this->collection->update($criteria, $updates, ['multiple' => true]);
             $rows = static::processResult($result);
             if (0 === $rows) {
                 throw new NotFoundException('No requested records were found to update.');
             }
             if (count($this->batchIds) !== $rows) {
                 throw new BadRequestException('Batch Error: Not all requested records were found to update.');
             }
             if ($requireMore) {
                 $fieldArray = static::buildFieldArray($fields);
                 /** @var \MongoCursor $result */
                 $result = $this->collection->find($criteria, $fieldArray);
                 $out = static::cleanRecords(iterator_to_array($result));
             } else {
                 $out = static::idsAsRecords(static::mongoIdsToIds($this->batchIds), static::DEFAULT_ID_FIELD);
             }
             break;
         case Verbs::MERGE:
         case Verbs::PATCH:
             if (empty($updates)) {
                 throw new BadRequestException('Batch operation not supported for patch by records.');
             }
             $updates = ['$set' => $updates];
             $filter = [static::DEFAULT_ID_FIELD => ['$in' => $this->batchIds]];
             $criteria = static::buildCriteriaArray($filter, null, $ssFilters);
             $result = $this->collection->update($criteria, $updates, ['multiple' => true]);
             $rows = static::processResult($result);
             if (0 === $rows) {
                 throw new NotFoundException('No requested records were found to patch.');
             }
             if (count($this->batchIds) !== $rows) {
                 throw new BadRequestException('Batch Error: Not all requested records were found to patch.');
             }
             if ($requireMore) {
                 $fieldArray = static::buildFieldArray($fields);
                 /** @var \MongoCursor $result */
                 $result = $this->collection->find($criteria, $fieldArray);
                 $out = static::cleanRecords(iterator_to_array($result));
             } else {
                 $out = static::idsAsRecords(static::mongoIdsToIds($this->batchIds), static::DEFAULT_ID_FIELD);
             }
             break;
         case Verbs::DELETE:
             $filter = [static::DEFAULT_ID_FIELD => ['$in' => $this->batchIds]];
             $criteria = static::buildCriteriaArray($filter, null, $ssFilters);
             if ($requireMore) {
                 $fieldArray = static::buildFieldArray($fields);
                 $result = $this->collection->find($criteria, $fieldArray);
                 $result = static::cleanRecords(iterator_to_array($result));
                 if (empty($result)) {
                     throw new NotFoundException('No records were found using the given identifiers.');
                 }
                 if (count($this->batchIds) !== count($result)) {
                     $errors = [];
                     foreach ($this->batchIds as $index => $id) {
                         $found = false;
                         foreach ($result as $record) {
                             if ($id == ArrayUtils::get($record, static::DEFAULT_ID_FIELD)) {
                                 $out[$index] = $record;
                                 $found = true;
                                 continue;
                             }
                         }
                         if (!$found) {
                             $errors[] = $index;
                             $out[$index] = "Record with identifier '" . print_r($id, true) . "' not found.";
                         }
                     }
                 } else {
                     $out = $result;
                 }
             } else {
                 $out = static::idsAsRecords(static::mongoIdsToIds($this->batchIds), static::DEFAULT_ID_FIELD);
             }
             $result = $this->collection->remove($criteria);
             $rows = static::processResult($result);
             if (0 === $rows) {
                 throw new NotFoundException('No records were found using the given identifiers.');
             }
             if (count($this->batchIds) !== $rows) {
                 throw new BadRequestException('Batch Error: Not all requested records were deleted.');
             }
             break;
         case Verbs::GET:
             $filter = [static::DEFAULT_ID_FIELD => ['$in' => $this->batchIds]];
             $criteria = static::buildCriteriaArray($filter, null, $ssFilters);
             $fieldArray = static::buildFieldArray($fields);
             $result = $this->collection->find($criteria, $fieldArray);
             $result = static::cleanRecords(iterator_to_array($result));
             if (empty($result)) {
                 throw new NotFoundException('No records were found using the given identifiers.');
             }
             if (count($this->batchIds) !== count($result)) {
                 $errors = [];
                 foreach ($this->batchIds as $index => $id) {
                     $found = false;
                     foreach ($result as $record) {
                         if ($id == ArrayUtils::get($record, static::DEFAULT_ID_FIELD)) {
                             $out[$index] = $record;
                             $found = true;
                             continue;
                         }
                     }
                     if (!$found) {
                         $errors[] = $index;
                         $out[$index] = "Record with identifier '" . print_r($id, true) . "' not found.";
                     }
                 }
                 if (!empty($errors)) {
                     $wrapper = ResourcesWrapper::getWrapper();
                     $context = ['error' => $errors, $wrapper => $out];
                     throw new NotFoundException('Batch Error: Not all records could be retrieved.', null, null, $context);
                 }
             }
             $out = $result;
             break;
         default:
             break;
     }
     $this->batchIds = [];
     $this->batchRecords = [];
     return $out;
 }