MongoCollection::batchInsert PHP Method

batchInsert() public method

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.
return 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.
    public function batchInsert(array &$a, array $options = [])
    {
        if (empty($a)) {
            throw new \MongoException('No write ops were included in the batch');
        }
        $continueOnError = isset($options['continueOnError']) && $options['continueOnError'];
        foreach ($a as $key => $item) {
            try {
                if (!$this->ensureDocumentHasMongoId($a[$key])) {
                    if ($continueOnError) {
                        unset($a[$key]);
                    } else {
                        trigger_error(sprintf('%s expects parameter %d to be an array or object, %s given', __METHOD__, 1, gettype($a)), E_USER_WARNING);
                        return;
                    }
                }
            } catch (MongoException $e) {
                if (!$continueOnError) {
                    throw $e;
                }
            }
        }
        try {
            $result = $this->collection->insertMany(TypeConverter::fromLegacy(array_values($a)), $this->convertWriteConcernOptions($options));
        } catch (\MongoDB\Driver\Exception\Exception $e) {
            throw ExceptionConverter::toLegacy($e, 'MongoResultException');
        }
        if (!$result->isAcknowledged()) {
            return true;
        }
        return ['ok' => 1.0, 'connectionId' => 0, 'n' => 0, 'syncMillis' => 0, 'writtenTo' => null, 'err' => null];
    }

Usage Example

Beispiel #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();
 }
All Usage Examples Of MongoCollection::batchInsert