public function Insert($collection, $data) { //check for input and get an associative array if (!is_string($collection) || strlen($collection) < 3 || strpos($collection, '.') < 1) { throw new \InvalidArgumentException('The collection name to be filled on the database must be given as "database.collection"'); } if (!is_array($data) && !$data instanceof CollectionInterface) { throw new \InvalidArgumentException('The data to be written on the database must be given as a collection'); } $adaptedData = $data instanceof GenericCollection ? $data->all() : $data; //create a bulkwriter and fill it $bulk = new \MongoDB\Driver\BulkWrite(); //execute the write operation try { $nativeID = $bulk->insert($adaptedData); //$writeConcern = new \MongoDB\Driver\WriteConcern(\MongoDB\Driver\WriteConcern::MAJORITY, 1000); $result = $this->connection['db_manager']->executeBulkWrite($collection, $bulk); } catch (\MongoDB\Driver\Exception\BulkWriteException $ex) { throw new DatabaseException('Insertion failed due to a write error', 3); } catch (\MongoDB\Driver\Exception\InvalidArgumentException $ex) { throw new DatabaseException('Insertion failed due to an error occurred while parsing data', 3); } catch (\MongoDB\Driver\Exception\ConnectionException $ex) { throw new DatabaseException('Insertion failed due to an error on authentication', 3); } catch (\MongoDB\Driver\Exception\AuthenticationException $ex) { throw new DatabaseException('Insertion failed due to an error on connection', 3); } catch (\MongoDB\Driver\Exception\RuntimeException $ex) { throw new DatabaseException('Insertion failed due to an unknown error', 3); } //check for write errors if ($result->getInsertedCount() <= 0) { throw new DatabaseException('Insertion failed due to an unknown error', 3); } //return the MongoDB Object ID return new MongodbObjectID($nativeID, $collection); }
function setRegistrosMasivos($_n_competiciones = 100) { $bulk = new MongoDB\Driver\BulkWrite(); $total = $_n_competiciones < 0 || $_n_competiciones > ModeloBBDD::MAX_REGISTROS_ADD ? ModeloBBDD::MAX_REGISTROS_ADD : $_n_competiciones; $i = 0; for (; $i < $total; $i++) { $bulk->insert(['x' => $i . "-" . rand(0, $total)]); } $this->conexion->executeBulkWrite('mensajes.holamundo', $bulk); }
public function insert(array $document) { // Get options $options = func_num_args() > 1 ? (array) func_get_arg(1) : array(); // Insert the new document try { $bulk = new \MongoDB\Driver\BulkWrite(); $id = (string) $bulk->insert($document); $this->database->getDriver()->executeBulkWrite($this->name, $bulk); } catch (\Exception $e) { throw new Exception($e->getMessage()); } return $id; }
/** * Insert pre-validated documents into database * * @global type $db * @param type $data */ public function Insert($data) { global $db; $cn = self::CollectionName(); //Remove any preset IDs !IMPORTANT! unset($data->{'_id'}); $bw = new MongoDB\Driver\BulkWrite(); $bw->insert($data); $result = $db->executeBulkWrite($cn, $bw); $response = []; $response['editor_action'] = 'reload'; $response['db_output'] = $result; return $response; }
function LOAD($uri, $dbname = DATABASE_NAME, $collname = COLLECTION_NAME, $filename = null) { if (!$filename) { $filename = "compress.zlib://" . __DIR__ . "/" . "PHONGO-FIXTURES.json.gz"; } $manager = new MongoDB\Driver\Manager($uri); $bulk = new MongoDB\Driver\BulkWrite(['ordered' => false]); $server = $manager->selectServer(new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_PRIMARY)); $data = file_get_contents($filename); $array = json_decode($data); foreach ($array as $document) { $bulk->insert($document); } $retval = $server->executeBulkWrite("{$dbname}.{$collname}", $bulk); if ($retval->getInsertedCount() !== count($array)) { exit(sprintf('skip Fixtures were not loaded (expected: %d, actual: %d)', $total, $retval->getInsertedCount())); } }
<?php $m = new MongoDB\Driver\Manager('mongodb://localhost:27017'); $b = new MongoDB\Driver\BulkWrite(); $doc = ['_id' => new MongoDB\BSON\ObjectID(), 'name' => 'runnoob']; $_id = $b->insert($doc); var_dump($_id); $write = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000); $res = $m->executeBulkWrite('test.runnoob', $b, $write); echo 'createCollection success!';
/** * @covers Swift_MongoDbSpool::flushQueue * @covers Swift_MongoDbSpool::find * @covers Swift_MongoDbSpool::delete * @covers Swift_MongoDbSpool::write * @covers Swift_MongoDbSpool::setSentOn * @covers Swift_MongoDbSpool::now */ public function testFlushQueue() { $body = 'Here is the message itself'; $message1 = Swift_Message::newInstance()->setSubject('Your subject 1')->setFrom(array('*****@*****.**' => 'John Doe'))->setTo(array('*****@*****.**'))->setBody($body . ' 1'); $message2 = Swift_Message::newInstance()->setSubject('Your subject 2')->setFrom(array('*****@*****.**' => 'John Doe'))->setTo(array('*****@*****.**'))->setBody($body . ' 2'); $bulk = new \MongoDB\Driver\BulkWrite(array('ordered' => true)); $bulk->insert(array('message' => new \MongoDB\BSON\Binary(serialize($message1), \MongoDB\BSON\Binary::TYPE_GENERIC), 'sentOn' => null)); $bulk->insert(array('message' => null, 'sentOn' => null)); $bulk->insert(array('message' => new \MongoDB\BSON\Binary(serialize($message2), \MongoDB\BSON\Binary::TYPE_GENERIC), 'sentOn' => null)); $bulk->insert(array('message' => 'hi', 'sentOn' => null)); $wc = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000); $this->manager->executeBulkWrite($this->getNamespace(), $bulk, $wc); $object = new Swift_MongoDbSpool($this->manager, $this->getNamespace(), null, $wc); $failed = array(); $transport = $this->getMockBuilder('Swift_Transport')->getMock(); $transport->method('isStarted')->willReturn(true); $transport->expects($this->exactly(2))->method('send')->withConsecutive(array($this->equalTo($message1), $this->equalTo($failed)), array($this->equalTo($message2), $this->equalTo($failed)))->willReturn(1); $this->assertEquals(4, $this->docount(array())); $this->assertEquals(2, $object->flushQueue($transport, $failed)); $this->assertEquals(2, $this->docount(array())); $this->verifyMockObjects(); }
/** * 批量添加 * @param array $documents * @param string $wstring * @param int $wtimeout * @return mixed */ public function batch_insert($documents = array(), $wstring = \MongoDB\Driver\WriteConcern::MAJORITY, $wtimeout = 1000) { try { $wc = new \MongoDB\Driver\WriteConcern($wstring, $wtimeout); $bulk = new \MongoDB\Driver\BulkWrite(); foreach ($documents as $k => $document) { $bulk->insert($document); } $dbc = $this->database . '.' . $this->collection; $result = $this->manager->executeBulkWrite($dbc, $bulk, $wc); $this->result = $result; //增加几条 return $result->getInsertedCount(); } catch (\Exception $e) { $this->showError($e); } }
public function insert($tname, $data) { $b = new MongoDB\Driver\BulkWrite(); $b->insert($data); return $this->mongo->executeBulkWrite($this->getCName($tname), $b, $this->w); }
/** * Queues a message. * * @param Swift_Mime_Message $message The message to store * @throws Swift_IoException * @return bool */ public function queueMessage(Swift_Mime_Message $message) { $bulk = new \MongoDB\Driver\BulkWrite(); $bulk->insert(array('message' => new \MongoDB\BSON\Binary(serialize($message), \MongoDB\BSON\Binary::TYPE_GENERIC))); $this->write($bulk); return true; }
public function createAndLoginClient() { if (!$this->client) { $faker = $this->getFaker(); $client = ['client_id' => md5($faker->name), 'client_secret' => $faker->password(32, 32)]; $bulk = new MongoDB\Driver\BulkWrite(); $bulk->insert($client); $this->getMongo()->executeBulkWrite('gandalf_test.oauth_clients', $bulk); $this->client = $client; } $this->loginClient($this->client); return $this->client; }