Gets the underlying collection for this object
public getCollection ( ) : MongoDB\Collection | ||
return | MongoDB\Collection |
/** * Executes a batch of write operations * * @see http://php.net/manual/en/mongowritebatch.execute.php * @param array $writeOptions * @return array */ public final function execute(array $writeOptions = []) { $writeOptions += $this->writeOptions; if (!count($this->items)) { return ['ok' => true]; } if (isset($writeOptions['j'])) { trigger_error('j parameter is not supported', E_WARNING); } if (isset($writeOptions['fsync'])) { trigger_error('fsync parameter is not supported', E_WARNING); } $options['writeConcern'] = $this->createWriteConcernFromArray($writeOptions); if (isset($writeOptions['ordered'])) { $options['ordered'] = $writeOptions['ordered']; } $collection = $this->collection->getCollection(); try { $result = $collection->BulkWrite($this->items, $options); $ok = 1.0; } catch (\MongoDB\Driver\Exception\BulkWriteException $e) { $result = $e->getWriteResult(); $ok = 0.0; } if ($ok === 1.0) { $this->items = []; } return ['ok' => $ok, 'nInserted' => $result->getInsertedCount(), 'nMatched' => $result->getMatchedCount(), 'nModified' => $result->getModifiedCount(), 'nUpserted' => $result->getUpsertedCount(), 'nRemoved' => $result->getDeletedCount()]; }
/** * Model contructor * * Build the model */ public function __construct($doc, $collection) { // New document if ($doc === null) { $doc = array(); } // Validate doc if (!is_array($doc)) { throw new \RuntimeException('$data must be array.'); } if (!isset($doc['_id']) || $doc['_id'] instanceof \MongoId) { $this->_doc = $doc; } else { foreach ($doc as $k => $v) { $this->_setter($k, $v); } } // Validate collection $this->_collection = $collection; if ($collection instanceof \MongoCollection) { $collection = new MongoCollection($collection); $this->_collection = $collection->getCollection(); } else { if ($collection instanceof Adapters\MongoCollection) { $this->_collection = $collection->getCollection(); } else { if (!$collection instanceof Collection) { throw new \RuntimeException('$collection must be an instance of Modomo\\Collection or Modomo\\Adapters\\MongoCollection or MongoCollection'); } } } // New model $this->_new = true; if (isset($doc['_id']) && $doc['_id'] instanceof \MongoId) { $this->_new = false; $this->_collection->triggerEvent('onCreateNew', $this); } else { $this->_collection->triggerEvent('onCreate', $this); } }
/** * Executes a batch of write operations * * @see http://php.net/manual/en/mongowritebatch.execute.php * @param array $writeOptions * @return array */ public final function execute(array $writeOptions = []) { $writeOptions += $this->writeOptions; if (!count($this->items)) { return ['ok' => true]; } if (isset($writeOptions['j'])) { trigger_error('j parameter is not supported', E_WARNING); } if (isset($writeOptions['fsync'])) { trigger_error('fsync parameter is not supported', E_WARNING); } $options['writeConcern'] = $this->createWriteConcernFromArray($writeOptions); if (isset($writeOptions['ordered'])) { $options['ordered'] = $writeOptions['ordered']; } try { $writeResult = $this->collection->getCollection()->bulkWrite($this->items, $options); $resultDocument = []; $ok = true; } catch (BulkWriteException $e) { $writeResult = $e->getWriteResult(); $resultDocument = ['writeErrors' => $this->convertWriteErrors($writeResult)]; $ok = false; } $this->items = []; switch ($this->batchType) { case self::COMMAND_UPDATE: $upsertedIds = []; foreach ($writeResult->getUpsertedIds() as $index => $id) { $upsertedIds[] = ['index' => $index, '_id' => TypeConverter::toLegacy($id)]; } $resultDocument += ['nMatched' => $writeResult->getMatchedCount(), 'nModified' => $writeResult->getModifiedCount(), 'nUpserted' => $writeResult->getUpsertedCount(), 'ok' => true]; if (count($upsertedIds)) { $resultDocument['upserted'] = $upsertedIds; } break; case self::COMMAND_DELETE: $resultDocument += ['nRemoved' => $writeResult->getDeletedCount(), 'ok' => true]; break; case self::COMMAND_INSERT: $resultDocument += ['nInserted' => $writeResult->getInsertedCount(), 'ok' => true]; break; } if (!$ok) { // Exception code is hardcoded to the value in ext-mongo, see // https://github.com/mongodb/mongo-php-driver-legacy/blob/ab4bc0d90e93b3f247f6bcb386d0abc8d2fa7d74/batch/write.c#L428 throw new \MongoWriteConcernException('Failed write', 911, null, $resultDocument); } return $resultDocument; }
/** * Executes a batch of write operations * * @see http://php.net/manual/en/mongowritebatch.execute.php * @param array $writeOptions * @return array */ public final function execute(array $writeOptions = []) { $writeOptions += $this->writeOptions; if (!count($this->items)) { return ['ok' => true]; } if (isset($writeOptions['j'])) { trigger_error('j parameter is not supported', E_WARNING); } if (isset($writeOptions['fsync'])) { trigger_error('fsync parameter is not supported', E_WARNING); } $options['writeConcern'] = $this->createWriteConcernFromArray($writeOptions); if (isset($writeOptions['ordered'])) { $options['ordered'] = $writeOptions['ordered']; } $collection = $this->collection->getCollection(); try { $result = $collection->BulkWrite($this->items, $options); $ok = true; } catch (\MongoDB\Driver\Exception\BulkWriteException $e) { $result = $e->getWriteResult(); $ok = false; } if ($ok === true) { $this->items = []; } switch ($this->batchType) { case self::COMMAND_UPDATE: $upsertedIds = []; foreach ($result->getUpsertedIds() as $index => $id) { $upsertedIds[] = ['index' => $index, '_id' => TypeConverter::toLegacy($id)]; } $result = ['nMatched' => $result->getMatchedCount(), 'nModified' => $result->getModifiedCount(), 'nUpserted' => $result->getUpsertedCount(), 'ok' => $ok]; if (count($upsertedIds)) { $result['upserted'] = $upsertedIds; } return $result; case self::COMMAND_DELETE: return ['nRemoved' => $result->getDeletedCount(), 'ok' => $ok]; case self::COMMAND_INSERT: return ['nInserted' => $result->getInsertedCount(), 'ok' => $ok]; } }