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()
public batchInsert ( array $rows, $validate = true ) : |
||
$rows | array | list of documents to insert, defined as arrays |
return |
public function testDelete() { // insert $this->collection->batchInsert(array(array('a' => 1), array('a' => 2), array('a' => 3), array('a' => 4), array('a' => 5), array('a' => 6))); // delete $batch = new BatchDelete($this->collection); $batch->delete(array('a' => 2))->delete($this->collection->expression()->where('a', 4))->delete(function (Expression $e) { $e->where('a', 6); })->execute(); // test $result = $this->collection->findAsArray()->sort(array('a' => 1))->map(function ($data) { unset($data['_id']); return $data; }); $this->assertEquals(array_values($result), array(array('a' => 1), array('a' => 3), array('a' => 5))); }
public function testUnwind() { // add documents $this->collection->batchInsert(array(array('subdoc' => array('key' => array(1, 2))), array('subdoc' => array('key' => array(4, 8))))); // create pipeline $pipeline = new Pipeline($this->collection); $pipeline->unwind('$subdoc.key'); // get result $result = $pipeline->aggregate(); $this->assertEquals(4, count($result)); $this->assertEquals(1, $result[0]['subdoc']['key']); $this->assertEquals(2, $result[1]['subdoc']['key']); $this->assertEquals(4, $result[2]['subdoc']['key']); $this->assertEquals(8, $result[3]['subdoc']['key']); $this->assertTrue((string) $result[0]['_id'] === (string) $result[1]['_id']); $this->assertTrue((string) $result[2]['_id'] === (string) $result[3]['_id']); $this->assertTrue((string) $result[0]['_id'] !== (string) $result[2]['_id']); }
public function testAggregate_Callable() { $this->collection->batchInsert(array(array('order' => 1, 'item' => 1, 'amount' => 110, 'category' => 1), array('order' => 1, 'item' => 2, 'amount' => 120, 'category' => 1), array('order' => 1, 'item' => 3, 'amount' => 130, 'category' => 2), array('order' => 2, 'item' => 1, 'amount' => 210, 'category' => 1), array('order' => 2, 'item' => 2, 'amount' => 220, 'category' => 1), array('order' => 2, 'item' => 3, 'amount' => 230, 'category' => 2))); $result = $this->collection->aggregate(function ($pipeline) { /* @var $pipeline \Sokil\Mongo\Pipeline */ $pipeline->match(array('category' => 1))->group(array('_id' => '$order', 'totalAmount' => array('$sum' => '$amount')))->sort(array('_id' => 1)); }); $this->assertEquals(array(0 => array('_id' => 1, 'totalAmount' => 230), 1 => array('_id' => 2, 'totalAmount' => 430)), $result); }
public function testPush() { date_default_timezone_set('Europe/Kiev'); $this->collection->batchInsert(array(array("_id" => 1, "item" => "abc", "price" => 10, "quantity" => 2, "date" => new \MongoDate(strtotime("2014-01-01T08:00:00Z"))), array("_id" => 2, "item" => "jkl", "price" => 20, "quantity" => 1, "date" => new \MongoDate(strtotime("2014-02-03T09:00:00Z"))), array("_id" => 3, "item" => "xyz", "price" => 5, "quantity" => 5, "date" => new \MongoDate(strtotime("2014-02-03T09:05:00Z"))), array("_id" => 4, "item" => "abc", "price" => 10, "quantity" => 10, "date" => new \MongoDate(strtotime("2014-02-15T08:00:00Z"))), array("_id" => 5, "item" => "xyz", "price" => 5, "quantity" => 10, "date" => new \MongoDate(strtotime("2014-02-15T09:05:00Z"))), array("_id" => 6, "item" => "xyz", "price" => 5, "quantity" => 5, "date" => new \MongoDate(strtotime("2014-02-15T12:05:10Z"))), array("_id" => 7, "item" => "xyz", "price" => 5, "quantity" => 10, "date" => new \MongoDate(strtotime("2014-02-15T14:12:12Z"))))); $pipeline = new Pipeline($this->collection); $pipeline->group(function ($stage) { /* @var $stage \Sokil\Mongo\Pipeline\GroupStage */ $stage->setId('$item')->push('itemsSold', array('quantity' => '$quantity', 'price' => '$price', 'quantityPrice' => array('$multiply' => array('$quantity', '$price')))); }); $result = $pipeline->aggregate(); $this->assertEquals('xyz', $result[0]['_id']); $this->assertEquals(5, $result[0]['itemsSold'][0]['quantity']); $this->assertEquals(5, $result[0]['itemsSold'][0]['price']); $this->assertEquals(25, $result[0]['itemsSold'][0]['quantityPrice']); }
public function testGetDocuments_AllFromPool() { $doc1Id = new \MongoId(); $doc2Id = new \MongoId(); $doc3Id = new \MongoId(); // add documents skipping document pool $this->collection->batchInsert(array(array('_id' => $doc1Id, 'param' => 'value1'), array('_id' => $doc2Id, 'param' => 'value2'), array('_id' => $doc3Id, 'param' => 'value3'))); // load documents to document pool $doc1 = $this->collection->getDocument($doc1Id); $this->assertEquals('value1', $doc1->param); $doc2 = $this->collection->getDocument($doc2Id); $this->assertEquals('value2', $doc2->param); $doc3 = $this->collection->getDocument($doc3Id); $this->assertEquals('value3', $doc3->param); $this->assertEquals(3, $this->collection->documentPoolCount()); // load all documents $documents = $this->collection->getDocuments(array($doc1Id, $doc2Id, $doc3Id)); $this->assertArrayHasKey((string) $doc1Id, $documents); $this->assertArrayHasKey((string) $doc2Id, $documents); $this->assertArrayHasKey((string) $doc3Id, $documents); }