本文整理汇总了PHP中MongoCollection::batchInsert方法的典型用法代码示例。如果您正苦于以下问题:PHP MongoCollection::batchInsert方法的具体用法?PHP MongoCollection::batchInsert怎么用?PHP MongoCollection::batchInsert使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MongoCollection
的用法示例。
在下文中一共展示了MongoCollection::batchInsert方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: executeInserts
/**
* 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();
}
示例2: doBatchInsert
/**
* 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);
}
示例3: batchInsert
/**
* 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;
}
示例4: batchInsert
/**
* 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;
}
示例5: batchPersist
/**
* {@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']);
}
}
示例6: testSafeRemove2
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']);
}
示例7: mongodb_fixture
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;
}
示例8: testBatchInsert
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']);
}
示例9: insert
/**
* 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'];
}
示例10: executeInserts
/**
* 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();
}
示例11: insert
/**
* 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'];
}
示例12: batchInsert
/**
* 批量插入一组新的数据
*
* @param array $array 每一个元素包含一个要插入的行
* @return boolean
*/
function batchInsert(array $array)
{
return $this->_collection->batchInsert($array);
}
示例13: batchInsert
/**
* 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;
}
示例14: multi_exec
/**
* 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;
}
示例15: batchInsert
/**
* 批量插入数据
*
* @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);
}