See also: http://php.net/manual/en/class.mongoinsertbatch.php
See also: http://php.net/manual/en/class.mongowritebatch.php
Inheritance: extends MongoWriteBatch
コード例 #1
0
 public function testInsertBatchError()
 {
     $collection = $this->getCollection();
     $batch = new \MongoInsertBatch($collection);
     $collection->createIndex(['foo' => 1], ['unique' => true]);
     $this->assertTrue($batch->add(['foo' => 'bar']));
     $this->assertTrue($batch->add(['foo' => 'bar']));
     $expected = ['ok' => 0.0, 'nInserted' => 1, 'nMatched' => 0, 'nModified' => 0, 'nUpserted' => 0, 'nRemoved' => 0];
     $this->assertSame($expected, $batch->execute());
 }
コード例 #2
0
 public function testInsertBatchError()
 {
     $collection = $this->getCollection();
     $batch = new \MongoInsertBatch($collection);
     $collection->createIndex(['foo' => 1], ['unique' => true]);
     $this->assertTrue($batch->add(['foo' => 'bar']));
     $this->assertTrue($batch->add(['foo' => 'bar']));
     $expected = ['writeErrors' => [['index' => 1, 'code' => 11000]], 'nInserted' => 1, 'ok' => true];
     try {
         $batch->execute();
     } catch (\MongoWriteConcernException $e) {
         $this->assertSame('Failed write', $e->getMessage());
         $this->assertArraySubset($expected, $e->getDocument());
     }
 }
コード例 #3
0
ファイル: MongoWrapper.php プロジェクト: AndyNiu/commonswoole
 /**
  * 批量插入, 目前不建议使用, 有未知错误
  * @param $data
  */
 public function batchInsert($data = array())
 {
     if ($data || is_array($data)) {
         try {
             $arr =& $this->_getBatchArr('insert');
             if (count($arr) > self::MAXBATCHNUM) {
                 $c = $this->_getColctObj();
                 $batch = new MongoInsertBatch($c, array('ordered' => false));
                 foreach ($arr as $doc) {
                     $batch->add((object) $doc);
                 }
                 $batch->execute();
                 $arr = array();
             } else {
                 array_push($arr, $data);
             }
         } catch (MongoException $e) {
             // 这里插入重复,不做特殊处理
             Logger::logInfo("error message: " . $e->getMessage() . "error code: [" . $e->getCode() . "]");
         }
     } else {
         throw new \Exception('date of batchInsert is invaild');
     }
 }
コード例 #4
0
ファイル: Collection.php プロジェクト: ngchie/system
 /**
  * method to bulk insert of multiple documents
  * 
  * @param array $a array or object. If an object is used, it may not have protected or private properties
  * @param array $options options for the inserts.; see php documentation
  * 
  * @return mixed 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"). Otherwise, returns TRUE if the batch insert was successfully sent, FALSE otherwise
  * @see http://php.net/manual/en/mongocollection.batchinsert.php
  */
 public function batchInsert(array $a, array $options = array())
 {
     if (!isset($options['w'])) {
         $options['w'] = $this->w;
     }
     if (!isset($options['j'])) {
         $options['j'] = $this->j;
     }
     if ($this->_db->compareServerVersion('2.6', '>=') && $this->_db->compareClientVersion('1.5', '>=')) {
         $batch = new MongoInsertBatch($this->_collection);
         foreach ($a as $doc) {
             $batch->add($doc);
         }
         return $batch->execute($options);
     } else {
         return $this->_collection->batchInsert($a, $options);
     }
 }
コード例 #5
0
ファイル: MongoDBUtil.class.php プロジェクト: rajnishp/bjs
 public function multiInsert($data, $writeOptions = array('w' => 1))
 {
     global $logger;
     $logger->debug("Mongo multiInsert() :: Data: " . json_encode($data));
     $logger->debug("Mongo multiInsert() :: Option: " . json_encode($writeOptions));
     $batch = new MongoInsertBatch($this->collection, $writeOptions);
     foreach ($data as $document) {
         $batch->add($document);
     }
     $result = $batch->execute($writeOptions);
     $logger->debug("Mongo multiInsert() :: Result: " . json_encode($result));
     return $result;
 }