/** * Write into database * * @param $collection * @param $command * @param $data * @return mixed */ public function write($collection, $command, $data) { // Make sure the database is connected $this->_connection or $this->connect(); // Set the last query $this->_last_query = $data; // Configurations $config = $this->_config; // Exec bulk command $bulk = new BulkWrite(); switch ($command) { case 'insert': $data['_id'] = new \MongoDB\BSON\ObjectID(); $bulk->insert($data); break; case 'update': $bulk->update($data[0], $data[1], $data[2]); break; case 'delete': $bulk->delete($data[0], $data[1]); break; } try { $writeConcern = new \MongoDB\Driver\WriteConcern(\MongoDB\Driver\WriteConcern::MAJORITY, 1000); $response = $this->_connection->executeBulkWrite($config['database'] . '.' . $collection, $bulk, $writeConcern); } catch (\MongoDB\Driver\Exception\BulkWriteException $e) { //print_r($e);die(); echo $e->getMessage(), "\n"; //exit; } return $response; }
/** * Create data fixtures. * * @param integer $n */ private function createFixtures($n) { $bulkWrite = new BulkWrite(true); for ($i = 1; $i <= $n; $i++) { $bulkWrite->insert(['_id' => $i, 'x' => (object) ['foo' => 'bar']]); } $result = $this->manager->executeBulkWrite($this->getNamespace(), $bulkWrite); $this->assertEquals($n, $result->getInsertedCount()); }
/** * Create data fixtures. * * @param integer $n */ private function createFixtures($n) { $bulkWrite = new BulkWrite(['ordered' => true]); for ($i = 1; $i <= $n; $i++) { $bulkWrite->insert(['_id' => $i, 'x' => (int) ($i . $i)]); } $result = $this->manager->executeBulkWrite($this->getNamespace(), $bulkWrite); $this->assertEquals($n, $result->getInsertedCount()); }
public function testDrop() { $bulkWrite = new BulkWrite(); $bulkWrite->insert(['x' => 1]); $writeResult = $this->manager->executeBulkWrite($this->getNamespace(), $bulkWrite); $this->assertEquals(1, $writeResult->getInsertedCount()); $commandResult = $this->database->drop(); $this->assertCommandSucceeded($commandResult); $this->assertCollectionCount($this->getNamespace(), 0); }
protected function write(array $record) { if (isset($this->collection)) { $this->collection->insertOne($record['formatted']); } if (isset($this->manager, $this->namespace)) { $bulk = new BulkWrite(); $bulk->insert($record["formatted"]); $this->manager->executeBulkWrite($this->namespace, $bulk); } }
/** * Execute the operation. * * @see Executable::execute() * @param Server $server * @return InsertOneResult */ public function execute(Server $server) { $bulk = new Bulk(); $insertedId = $bulk->insert($this->document); if ($insertedId === null) { // TODO: This may be removed if PHPC-382 is implemented $insertedId = is_array($this->document) ? $this->document['_id'] : $this->document->_id; } $writeConcern = isset($this->options['writeConcern']) ? $this->options['writeConcern'] : null; $writeResult = $server->executeBulkWrite($this->databaseName . '.' . $this->collectionName, $bulk, $writeConcern); return new InsertOneResult($writeResult, $insertedId); }
/** * {@inheritdoc} */ public function append(AggregateRootInterface $aggregate, $table) { if ($this->isNeedToSnapshot($table, $aggregate->getVersion())) { if ($this->has($aggregate->getId(), $aggregate->getVersion(), $table)) { throw new LogicException(sprintf('Snapshot for "%s" with id "%s" and version "%d" already exist.', $table, (string) $aggregate->getId(), $aggregate->getVersion())); } $record = $this->serialize($aggregate); $this->watch($table, $record); $bulk = new BulkWrite(); $record['_id'] = $bulk->insert($record); $this->conn->executeBulkWrite($this->getNamespace($table), $bulk); } }
/** * Execute the operation. * * @see Executable::execute() * @param Server $server * @return InsertOneResult */ public function execute(Server $server) { $options = []; if (isset($this->options['bypassDocumentValidation']) && \MongoDB\server_supports_feature($server, self::$wireVersionForDocumentLevelValidation)) { $options['bypassDocumentValidation'] = $this->options['bypassDocumentValidation']; } $bulk = new Bulk($options); $insertedId = $bulk->insert($this->document); if ($insertedId === null) { $insertedId = \MongoDB\extract_id_from_inserted_document($this->document); } $writeConcern = isset($this->options['writeConcern']) ? $this->options['writeConcern'] : null; $writeResult = $server->executeBulkWrite($this->databaseName . '.' . $this->collectionName, $bulk, $writeConcern); return new InsertOneResult($writeResult, $insertedId); }
/** * Execute the operation. * * @see Executable::execute() * @param Server $server * @return InsertOneResult */ public function execute(Server $server) { $options = []; if (isset($this->options['bypassDocumentValidation']) && \MongoDB\server_supports_feature($server, self::$wireVersionForDocumentLevelValidation)) { $options['bypassDocumentValidation'] = $this->options['bypassDocumentValidation']; } $bulk = new Bulk($options); $insertedId = $bulk->insert($this->document); if ($insertedId === null) { // TODO: This may be removed if PHPC-382 is implemented $insertedId = is_array($this->document) ? $this->document['_id'] : $this->document->_id; } $writeConcern = isset($this->options['writeConcern']) ? $this->options['writeConcern'] : null; $writeResult = $server->executeBulkWrite($this->databaseName . '.' . $this->collectionName, $bulk, $writeConcern); return new InsertOneResult($writeResult, $insertedId); }
public function testListDatabases() { $bulkWrite = new BulkWrite(); $bulkWrite->insert(['x' => 1]); $writeResult = $this->manager->executeBulkWrite($this->getNamespace(), $bulkWrite); $this->assertEquals(1, $writeResult->getInsertedCount()); $databases = $this->client->listDatabases(); $this->assertInstanceOf('MongoDB\\Model\\DatabaseInfoIterator', $databases); foreach ($databases as $database) { $this->assertInstanceOf('MongoDB\\Model\\DatabaseInfo', $database); } $that = $this; $this->assertDatabaseExists($this->getDatabaseName(), function (DatabaseInfo $info) use($that) { $that->assertFalse($info->isEmpty()); $that->assertGreaterThan(0, $info->getSizeOnDisk()); }); }
/** * Execute the operation. * * @see Executable::execute() * @param Server $server * @return InsertManyResult */ public function execute(Server $server) { $bulk = new Bulk(['ordered' => $this->options['ordered']]); $insertedIds = []; foreach ($this->documents as $i => $document) { $insertedId = $bulk->insert($document); if ($insertedId !== null) { $insertedIds[$i] = $insertedId; } else { // TODO: This may be removed if PHPC-382 is implemented $insertedIds[$i] = is_array($document) ? $document['_id'] : $document->_id; } } $writeConcern = isset($this->options['writeConcern']) ? $this->options['writeConcern'] : null; $writeResult = $server->executeBulkWrite($this->databaseName . '.' . $this->collectionName, $bulk, $writeConcern); return new InsertManyResult($writeResult, $insertedIds); }
/** * Create one or more indexes for the collection by inserting into the * "system.indexes" collection (MongoDB <2.6). * * @param Server $server * @param IndexInput[] $indexes */ private function executeLegacy(Server $server) { $bulk = new Bulk(true); foreach ($this->indexes as $index) { $bulk->insert($index); } $server->executeBulkWrite($this->databaseName . '.system.indexes', $bulk); }
/** * {@inheritdoc} */ public function append($table, DomainMessageInterface $message, $status, array $exception = null) { $serialized = $this->serialize($message); $record = array_merge($serialized, array('status' => $status, 'exception' => $exception)); $this->watch($table, $record); $bulk = new BulkWrite(); $record['_id'] = $bulk->insert($record); $this->conn->executeBulkWrite(sprintf('%s.%s', $this->db, $table), $bulk); }
/** * Write a message to the log. * * @param array $event Event data * @return void * @throws Exception\RuntimeException */ protected function doWrite(array $event) { if (null === $this->manager) { throw new Exception\RuntimeException('MongoDB\\Driver\\Manager must be defined'); } if (isset($event['timestamp']) && $event['timestamp'] instanceof DateTimeInterface) { $millis = (int) floor((double) $event['timestamp']->format('U.u') * 1000); $event['timestamp'] = new UTCDateTime($millis); } $bulkWrite = new BulkWrite(); $bulkWrite->insert($event); $this->manager->executeBulkWrite($this->database, $bulkWrite, $this->writeConcern); }
function insertThousandsRandomDocumentsIgnoringDuplicatesDemo() { $bulk = new MongoDB\Driver\BulkWrite(['ordered' => false]); collect(range(4400, 8000))->each(function ($i) use(&$bulk) { echo "Generating random document #{$i}\n"; $document = ["_id" => $this->generateRandomString(20), "timestamp" => rand(100000, 1000000), "network_site_id" => $this->generateRandomString(6), "request" => ["host" => $this->generateRandomString(16), "ip_country" => $this->generateRandomString(2), "path" => $this->generateRandomString(20), "method" => $this->generateRandomString(3), "status" => 200, "referrer" => $this->generateRandomString(50), "user_agent" => $this->generateRandomString(40), "session_id" => $this->generateRandomString(30)], "user" => ["id" => $this->generateRandomString(30) . '@gmailhotmailoryahoo.com'], "search_criteria" => ["minimum_price" => ["currency" => $this->generateRandomString(3), "EUR" => rand(50000, 500000), "GBP" => rand(50000, 500000), "USD" => rand(50000, 500000)], "page_size" => 15, "start_page" => rand(1, 80)], "results" => ["total" => rand(50, 1000), "starting_from" => rand(50, 1000), "finishing_at" => rand(50, 1000), "current_page" => rand(1, 50), "total_pages" => rand(1, 50)], "advert_ids" => [$this->generateRandomString(20), $this->generateRandomString(20), $this->generateRandomString(20), $this->generateRandomString(20), $this->generateRandomString(20), $this->generateRandomString(20), $this->generateRandomString(20), $this->generateRandomString(20), $this->generateRandomString(20), $this->generateRandomString(20), $this->generateRandomString(20), $this->generateRandomString(20), $this->generateRandomString(20), $this->generateRandomString(20), $this->generateRandomString(20)]]; //dd($document); $bulk->insert($document); }); try { echo "------- Executing bulk write\n"; $result = $this->mongo_manager->executeBulkWrite("db.props", $bulk, $this->write_concern); $bulk = new MongoDB\Driver\BulkWrite(['ordered' => false]); var_dump($result); } catch (MongoDB\Driver\Exception\Exception $e) { echo "Caught message: " . $e->getMessage(), "\n"; } }
/** * Create one or more indexes for the collection by inserting into the * "system.indexes" collection (MongoDB <2.6). * * @param Server $server */ private function executeLegacy(Server $server) { $bulk = new Bulk(['ordered' => true]); foreach ($this->indexes as $index) { $bulk->insert($index); } $server->executeBulkWrite($this->databaseName . '.system.indexes', $bulk, new WriteConcern(1)); }
public function testDateTypes() { //unix seconds: 1451649661 $mongo_date = new MongoDB\BSON\UTCDateTime(1451649661000 + 501.123456789); //01/01/2016 @ 12:01 + 500,000 microseconds echo "var dump: " . var_dump($mongo_date) . "\n"; echo "todatetime: " . var_dump($mongo_date->toDateTime()) . "\n"; echo "__tostring: |" . var_dump($mongo_date->__toString()) . "|\n"; $bulk_write = new MongoDB\Driver\BulkWrite(['ordered' => false]); $document = ["prop_id" => "leggett-FP-123456", "prop_type" => "residential", "timestamp" => ["utcdatetime" => $mongo_date, "year" => 2016, "month" => "01", "day" => "01", "hour" => "12", "minute" => "01"]]; $bulk_write->insert($document); $this->mongo_manager->executeBulkWrite($this->databaseAndCollectionName(), $bulk_write, $this->write_concern); }
/** * Execute the operation. * * @see Executable::execute() * @param Server $server * @return BulkWriteResult */ public function execute(Server $server) { $bulk = new Bulk(['ordered' => $this->options['ordered']]); $insertedIds = []; foreach ($this->operations as $i => $operation) { $type = key($operation); $args = current($operation); switch ($type) { case self::DELETE_MANY: case self::DELETE_ONE: $bulk->delete($args[0], $args[1]); break; case self::INSERT_ONE: $insertedId = $bulk->insert($args[0]); if ($insertedId !== null) { $insertedIds[$i] = $insertedId; } else { // TODO: This may be removed if PHPC-382 is implemented $insertedIds[$i] = is_array($args[0]) ? $args[0]['_id'] : $args[0]->_id; } break; case self::REPLACE_ONE: case self::UPDATE_MANY: case self::UPDATE_ONE: $bulk->update($args[0], $args[1], $args[2]); } } $writeConcern = isset($this->options['writeConcern']) ? $this->options['writeConcern'] : null; $writeResult = $server->executeBulkWrite($this->databaseName . '.' . $this->collectionName, $bulk, $writeConcern); return new BulkWriteResult($writeResult, $insertedIds); }
/** * 根据key 新增一条数据 * * @param string $table * @param array $data eg: array('username'=>'admin', 'email'=>'*****@*****.**') * * @return bool|int */ public function set($table, $data) { if (is_array($data)) { $bulk = new BulkWrite(); $insertId = $bulk->insert($data); $result = $this->runMongoBulkWrite($this->tablePrefix . $table, $bulk); $GLOBALS['debug'] && $this->debugLogSql('BulkWrite INSERT', $this->tablePrefix . $table, array(), $data); if ($result->getInsertedCount() > 0) { $this->lastInsertId = sprintf('%s', $insertId); } return $this->insertId(); } else { return false; } }
public function insert($namespace, $data = []) { $bulk = new BulkWrite(); $dataId = $bulk->insert($data) ?? $data['_id']; $this->result = $this->write($namespace, $bulk); return $this->result->getWriteErrors() ? false : $dataId; }
/** * 根据key 新增 一条数据 * * @param string $table * @param array $data eg: ['username'=>'admin', 'email'=>'*****@*****.**'] * @param mixed $tablePrefix 表前缀 不传则获取配置中配置的前缀 * * @return bool|int */ public function set($table, $data, $tablePrefix = null) { if (is_array($data)) { is_null($tablePrefix) && ($tablePrefix = $this->tablePrefix); $bulk = new BulkWrite(); $insertId = $bulk->insert($data); $result = $this->runMongoBulkWrite($tablePrefix . $table, $bulk); Cml::$debug && $this->debugLogSql('BulkWrite INSERT', $tablePrefix . $table, [], $data); if ($result->getInsertedCount() > 0) { $this->lastInsertId = sprintf('%s', $insertId); } return $this->insertId(); } else { return false; } }
/** * Execute the operation. * * @see Executable::execute() * * @param Server $server * * @return InsertManyResult */ public function execute(Server $server) { $options = ['ordered' => $this->options['ordered']]; if (isset($this->options['bypassDocumentValidation']) && Functions::serverSupportsFeature($server, self::$wireVersionForDocumentLevelValidation)) { $options['bypassDocumentValidation'] = $this->options['bypassDocumentValidation']; } $bulk = new Bulk($options); $insertedIds = []; foreach ($this->documents as $i => $document) { $insertedId = $bulk->insert($document); if ($insertedId !== null) { $insertedIds[$i] = $insertedId; } else { $insertedIds[$i] = Functions::extractIdFromInsertedDocument($document); } } $writeConcern = isset($this->options['writeConcern']) ? $this->options['writeConcern'] : null; $writeResult = $server->executeBulkWrite($this->databaseName . '.' . $this->collectionName, $bulk, $writeConcern); return new InsertManyResult($writeResult, $insertedIds); }
/** * Saves document in collection. * * @param array $arr * @param bool $validationNeeded * * @return ObjectID|Result */ public function insert(array $arr, $validationNeeded = true) { $result = new Result(); if ($validationNeeded) { $validatorResult = $this->validate($arr); if ($validatorResult->isValid() === false) { return $result->setError(Result::ERROR_VALIDATION_FAILED, $validatorResult->getErrorMessage()); } } $bulk = new BulkWrite(); $id = $bulk->insert($arr); $dbResult = $this->executeBulkWrite($bulk); if ($dbResult instanceof Exception) { $result->setError(Result::ERROR_CANNOT_INSERT_RECORD, $dbResult->getMessage()); } elseif ($dbResult->getInsertedCount() == 1) { $result = $id; $this->update(['_id' => $id], ['$currentDate' => ['lastModified' => true]]); } return $result; }
/** * Execute the operation. * * @see Executable::execute() * @param Server $server * @return BulkWriteResult */ public function execute(Server $server) { $options = ['ordered' => $this->options['ordered']]; if (isset($this->options['bypassDocumentValidation']) && \MongoDB\server_supports_feature($server, self::$wireVersionForDocumentLevelValidation)) { $options['bypassDocumentValidation'] = $this->options['bypassDocumentValidation']; } $bulk = new Bulk($options); $insertedIds = []; foreach ($this->operations as $i => $operation) { $type = key($operation); $args = current($operation); switch ($type) { case self::DELETE_MANY: case self::DELETE_ONE: $bulk->delete($args[0], $args[1]); break; case self::INSERT_ONE: $insertedId = $bulk->insert($args[0]); if ($insertedId !== null) { $insertedIds[$i] = $insertedId; } else { $insertedIds[$i] = \MongoDB\extract_id_from_inserted_document($args[0]); } break; case self::REPLACE_ONE: case self::UPDATE_MANY: case self::UPDATE_ONE: $bulk->update($args[0], $args[1], $args[2]); } } $writeConcern = isset($this->options['writeConcern']) ? $this->options['writeConcern'] : null; $writeResult = $server->executeBulkWrite($this->databaseName . '.' . $this->collectionName, $bulk, $writeConcern); return new BulkWriteResult($writeResult, $insertedIds); }
/** * Execute commands batch (bulk). * @param string $collectionName collection name. * @param array $options batch options. * @return array array of 2 elements: * * - 'insertedIds' - contains inserted IDs. * - 'result' - [[\MongoDB\Driver\WriteResult]] instance. * * @throws Exception on failure. * @throws InvalidConfigException on invalid [[document]] format. */ public function executeBatch($collectionName, $options = []) { $databaseName = $this->databaseName === null ? $this->db->defaultDatabaseName : $this->databaseName; $token = $this->log([$databaseName, $collectionName, 'bulkWrite'], $this->document, __METHOD__); try { $this->beginProfile($token, __METHOD__); $batch = new BulkWrite($options); $insertedIds = []; foreach ($this->document as $key => $operation) { switch ($operation['type']) { case 'insert': $insertedIds[$key] = $batch->insert($operation['document']); break; case 'update': $batch->update($operation['condition'], $operation['document'], $operation['options']); break; case 'delete': $batch->delete($operation['condition'], isset($operation['options']) ? $operation['options'] : []); break; default: throw new InvalidConfigException("Unsupported batch operation type '{$operation['type']}'"); } } $this->db->open(); $server = $this->db->manager->selectServer($this->getReadPreference()); $writeResult = $server->executeBulkWrite($databaseName . '.' . $collectionName, $batch, $this->getWriteConcern()); $this->endProfile($token, __METHOD__); } catch (RuntimeException $e) { $this->endProfile($token, __METHOD__); throw new Exception($e->getMessage(), $e->getCode(), $e); } return ['insertedIds' => $insertedIds, 'result' => $writeResult]; }
/** * Creates/Updates a collection based on the values in the atributes * * @return boolean|\Scene\Mvc\CollectionInterface * @throws Exception */ public function save() { $dependencyInjector = $this->_dependencyInjector; if (!is_object($dependencyInjector)) { throw new Exception('A dependency injector container is required to obtain the services related to the ORM'); } $db = $this->getDB(); $source = $this->getSource(); if (empty($source)) { throw new Exception('Method getSource() returns empty string'); } $data = $this->toArray(); /** * Check the dirty state of the current operation to update the current operation */ $exists = $this->_exists($this); $bulk = new BulkWrite(); if (!$exists) { $this->_operationMade = self::OP_CREATE; $id = $bulk->insert($data); } else { $this->_operationMade = self::OP_UPDATE; $filter = ['_id' => $this->_id]; $options = ['limit' => 1, 'upsert' => false]; $bulk->update($filter, $data, $options); } /** * The messages added to the validator are reset here */ $this->_errorMessages = []; $disableEvents = self::$_disableEvents; /** * Execute the preSave hook */ if ($this->_preSave($dependencyInjector, $disableEvents, $exists) === false) { return false; } $success = false; $result = $this->_collectionManager->executeBulkWrite($this, $db, $source, $bulk); if (empty($result->getWriteErrors())) { if (!$exists) { if ($result->getInsertedCount() > 0) { $this->_id = $id; $success = true; } } else { $success = true; } } /** * Call the postSave hooks */ return $this->_postSave($disableEvents, $success, $exists); }