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

insertMulti() public method

Inserts several documents
public insertMulti ( string $col, array $docs = [], callable $cb = null, array $params = [] ) : array
$col string Collection's name
$docs array Array of docs
$cb callable Optional. Callback
$params array Optional. Parameters
return array IDs
    public function insertMulti($col, $docs = [], $cb = null, $params = [])
    {
        if (mb_orig_strpos($col, '.') === false) {
            $col = $this->dbname . '.' . $col;
        }
        $ids = [];
        $bson = '';
        foreach ($docs as &$doc) {
            if (!isset($doc['_id'])) {
                $doc['_id'] = new MongoId();
            }
            try {
                $bson .= bson_encode($doc);
            } catch (\MongoException $e) {
                Daemon::log('MongoClient exception: ' . $e->getMessage() . ': ' . $e->getTraceAsString());
                if ($cb !== null) {
                    $cb(['$err' => $e->getMessage(), '$doc' => $doc]);
                }
            }
            $ids[] = $doc['_id'];
        }
        $this->request(self::OP_INSERT, "" . $col . "" . $bson, false, null, function ($conn, $reqId = null) use($cb, $col, $params) {
            if ($cb !== null) {
                $this->lastError($col, $cb, $params, $conn);
            }
        });
        return $ids;
    }