public function testUpsert() { $batch = new \MongoUpdateBatch($this->getCollection()); $this->assertTrue($batch->add(['q' => [], 'u' => ['$set' => ['foo' => 'bar']], 'upsert' => true])); $expected = ['ok' => 1.0, 'nInserted' => 0, 'nMatched' => 0, 'nModified' => 0, 'nUpserted' => 1, 'nRemoved' => 0]; $this->assertSame($expected, $batch->execute()); $newCollection = $this->getCheckDatabase()->selectCollection('test'); $this->assertSame(1, $newCollection->count()); $record = $newCollection->findOne(); $this->assertNotNull($record); $this->assertObjectHasAttribute('foo', $record); $this->assertAttributeSame('bar', 'foo', $record); }
public function testUpsert() { $document = ['foo' => 'foo']; $this->getCollection()->insert($document); $batch = new \MongoUpdateBatch($this->getCollection()); $this->assertTrue($batch->add(['q' => ['foo' => 'foo'], 'u' => ['$set' => ['foo' => 'bar']], 'upsert' => true])); $this->assertTrue($batch->add(['q' => ['bar' => 'foo'], 'u' => ['$set' => ['foo' => 'bar']], 'upsert' => true])); $expected = ['upserted' => [['index' => 1]], 'nMatched' => 1, 'nModified' => 1, 'nUpserted' => 1, 'ok' => true]; $result = $batch->execute(); $this->assertArraySubset($expected, $result); $this->assertInstanceOf('MongoId', $result['upserted'][0]['_id']); $newCollection = $this->getCheckDatabase()->selectCollection('test'); $this->assertSame(0, $newCollection->count(['foo' => 'foo'])); $this->assertSame(2, $newCollection->count()); $record = $newCollection->findOne(); $this->assertNotNull($record); $this->assertObjectHasAttribute('foo', $record); $this->assertAttributeSame('bar', 'foo', $record); }
/** * 批量操作后,取模结果余数内容的处理。 * @param * @return null */ public function flushBuffer() { $arr =& $this->_getBatchArr('update'); if (count($arr) > 0) { $c = $this->_getCollection(); $batch = new MongoUpdateBatch($c); foreach ($arr as $doc) { $batch->add((object) $doc); } $batch->execute(); } }
/** * Reverse the migrations. * * @return void */ public function down() { $db = \DB::getMongoDB(); $statementsCollection = new MongoCollection($db, 'statements'); $statementsCollection->deleteIndex('stored'); $statementsCollection->deleteIndex(['lrs_id' => 1, 'stored' => -1]); $statementsCollection->update([], ['$unset' => ["stored" => ""]], ['multiple' => true]); $statementsCursor = $statementsCollection->find(); $remaining = $statementsCursor->count(); print $remaining . ' statements total' . PHP_EOL; $maxBatchSize = 10000; while ($statementsCursor->hasNext()) { $batch = new MongoUpdateBatch($statementsCollection); $batchSize = 0; $shouldExecute = false; while ($batchSize < $maxBatchSize && $statementsCursor->hasNext()) { $batchSize++; $statement = $statementsCursor->next(); if (isset($statement['refs'])) { $query = ['q' => ['_id' => $statement['_id']], 'u' => ['$set' => []], 'multi' => false, 'upsert' => false]; foreach ($statement['refs'] as $key => $refStatement) { if (isset($refStatement['timestamp']) && $refStatement['timestamp'] instanceof MongoDate) { $query['u']['$set']['refs.' . $key . '.timestamp'] = date('Y-m-d\\TH:i:s.uP', $refStatement['timestamp']->sec); } if (isset($refStatement['stored']) && $refStatement['stored'] instanceof MongoDate) { $query['u']['$set']['refs.' . $key . '.stored'] = date('Y-m-d\\TH:i:s.uP', $refStatement['stored']->sec); } } if (!empty($query['u']['$set'])) { $batch->add((object) $query); $shouldExecute = true; } } } if ($shouldExecute) { $batch->execute(); } $remaining -= $batchSize; print $remaining . ' remaining' . PHP_EOL; } }
/** * @param $data */ public function batchUpdate($data = array()) { if ($data && is_array($data)) { try { $docs =& $this->_getBatchBuff('update'); if (count($docs) > self::MAXBATCHNUM) { $c = $this->_getColctObj(); $batch = new MongoUpdateBatch($c); foreach ($docs as $doc) { $batch->add((object) $doc); } $batch->execute(); $docs = array(); } else { if (isset($data['userkey'])) { $update = array('q' => array('userkey' => $data['userkey']), 'u' => array('$set' => $data), 'multi' => false, 'upsert' => true); array_push($docs, $update); } } } catch (MongoException $e) { Logger::logInfo('mongo in lib batchupdate failed, message [' . $e->getMessage() . '] code: [' . $e->getCode() . ']'); return false; } } else { Logger::logInfo('data of batchupdate is invaild'); return false; } }
public function multiUpdate($data, $writeOptions = array('w' => 1)) { global $logger; $logger->debug("Mongo multiUpdate() :: Data: " . json_encode($data)); $logger->debug("Mongo multiUpdate() :: Option: " . json_encode($writeOptions)); $batch = new MongoUpdateBatch($this->collection, $writeOptions); foreach ($data as $document) { $batch->add($document); } $result = $batch->execute($writeOptions); $logger->debug("Mongo multiUpdate() :: Result: " . json_encode($result)); return $result; }