PHPDaemon\Clients\Mongo\Pool::insert PHP Method

insert() public method

Inserts an object
public insert ( string $col, array $doc = [], callable $cb = null, array $params = [] ) : MongoId
$col string Collection's name
$doc array Document
$cb callable Optional. Callback
$params array Optional. Parameters
return MongoId
    public function insert($col, $doc = [], $cb = null, $params = [])
    {
        if (mb_orig_strpos($col, '.') === false) {
            $col = $this->dbname . '.' . $col;
        }
        if (!isset($doc['_id'])) {
            $doc['_id'] = new \MongoId();
        }
        if ($this->safeMode) {
            static::safeModeEnc($doc);
        }
        try {
            $this->request(self::OP_INSERT, "" . $col . "" . bson_encode($doc), false, null, function ($conn, $reqId = null) use($cb, $col, $params) {
                if ($cb !== null) {
                    $this->lastError($col, $cb, $params, $conn);
                }
            });
            return $doc['_id'];
        } catch (\MongoException $e) {
            Daemon::log('MongoClient exception: ' . $e->getMessage() . ': ' . $e->getTraceAsString());
            if ($cb !== null) {
                if ($cb !== null) {
                    $cb(['$err' => $e->getMessage(), '$doc' => $doc]);
                }
            }
        }
    }