Sokil\Mongo\Collection::insert PHP Method

insert() public method

Direct insert of array to MongoDB without creating document object and validation
public insert ( array $document ) : Collection
$document array
return Collection
    public function insert(array $document)
    {
        $result = $this->getMongoCollection()->insert($document);
        // if write concern acknowledged
        if (is_array($result)) {
            if ($result['ok'] != 1) {
                throw new Exception('Insert error: ' . $result['err'] . ': ' . $result['errmsg']);
            }
            return $this;
        }
        // if write concern unacknowledged
        if (!$result) {
            throw new Exception('Insert error');
        }
        return $this;
    }

Usage Example

Example #1
0
 public function testInsert_Unacknowledged()
 {
     $this->collection->setUnacknowledgedWriteConcern();
     $this->collection->insert(array('a' => 1, 'b' => 2));
     $document = $this->collection->find()->where('a', 1)->findOne();
     $this->assertNotEmpty($document);
     $this->assertEquals(2, $document->b);
 }
All Usage Examples Of Sokil\Mongo\Collection::insert