public function copyToCollection(
        $targetCollectionName,
        $targetDatabaseName = null,
        $batchLimit = 100
    ) {
        // target database
        if (empty($targetDatabaseName)) {
            $database = $this->collection->getDatabase();
        } else {
            $database = $this->client->getDatabase($targetDatabaseName);
        }

        // target collection
        $targetMongoCollection = $database
            ->getCollection($targetCollectionName)
            ->getMongoCollection();

        // cursor
        $this->rewind();

        // copy data
        $inProgress = true;
        while ($inProgress) {
            // get next pack of documents
            $documentList = array();
            for ($i = 0; $i < $batchLimit; $i++) {
                if (!$this->cursor->valid()) {
                    $inProgress = false;

                    if (!empty($documentList)) {
                        // still need batch insert
                        break;
                    } else {
                        // no documents to insert - just exit
                        break(2);
                    }
                }

                $documentList[] = $this->cursor->current();
                $this->cursor->next();
            }

            // insert
            $result = $targetMongoCollection->batchInsert($documentList);

            // With passed write concern, returns an associative array with the status of the inserts ("ok")
            // and any error that may have occurred ("err").
            // Otherwise, returns TRUE if the batch insert was successfully sent, FALSE otherwise.
            if (is_array($result)) {
                if ($result['ok'] != 1) {
                    throw new WriteException('Batch insert error: ' . $result['err']);
                }
            } elseif (false === $result) {
                throw new WriteException('Batch insert error');
            }
        }

        return $this;
    }
public function copyToCollection(
        $targetCollectionName,
        $targetDatabaseName = null,
        $batchLimit = 100
    ) {
        // target database
        if (empty($targetDatabaseName)) {
            $database = $this->collection->getDatabase();
        } else {
            $database = $this->client->getDatabase($targetDatabaseName);
        }

        // target collection
        $targetMongoCollection = $database
            ->getCollection($targetCollectionName)
            ->getMongoCollection();

        // cursor
        $this->rewind();

        // copy data
        $inProgress = true;
        while ($inProgress) {
            // get next pack of documents
            $documentList = array();
            for ($i = 0; $i < $batchLimit; $i++) {
                if (!$this->cursor->valid()) {
                    $inProgress = false;

                    if (!empty($documentList)) {
                        // still need batch insert
                        break;
                    } else {
                        // no documents to insert - just exit
                        break(2);
                    }
                }

                $documentList[] = $this->cursor->current();
                $this->cursor->next();
            }

            // insert
            $result = $targetMongoCollection->batchInsert($documentList);

            // With passed write concern, returns an associative array with the status of the inserts ("ok")
            // and any error that may have occurred ("err").
            // Otherwise, returns TRUE if the batch insert was successfully sent, FALSE otherwise.
            if (is_array($result)) {
                if ($result['ok'] != 1) {
                    throw new WriteException('Batch insert error: ' . $result['err']);
                }
            } elseif (false === $result) {
                throw new WriteException('Batch insert error');
            }
        }

        return $this;
    }
public function copyToCollection ( $ targetCollectionName , $ targetDatabaseName = null , $ batchLimit = 100 ) { // target database if ( empty ( $ targetDatabaseName ) ) { $ database = $ this -> collection -> getDatabase ( ) ; } else { $ database = $ this -> client -> getDatabase ( $ targetDatabaseName ) ; } // target collection $ targetMongoCollection = $ database -> getCollection ( $ targetCollectionName ) -> getMongoCollection ( ) ; // cursor $ this -> rewind ( ) ; // copy data $ inProgress = true ; while ( $ inProgress ) { // get next pack of documents $ documentList = array ( ) ; for ( $ i = 0 ; $ i < $ batchLimit ; $ i ++ ) { if ( ! $ this -> cursor -> valid ( ) ) { $ inProgress = false ; if ( ! empty ( $ documentList ) ) { // still need batch insert break ; } else { // no documents to insert - just exit break ( 2 ) ; } } $ documentList [ ] = $ this -> cursor -> current ( ) ; $ this -> cursor -> next ( ) ; } // insert $ result = $ targetMongoCollection -> batchInsert ( $ documentList ) ; // With passed write concern, returns an associative array with the status of the inserts ("ok") // and any error that may have occurred ("err"). // Otherwise, returns TRUE if the batch insert was successfully sent, FALSE otherwise. if ( is_array ( $ result ) ) { if ( $ result [ 'ok' ] != 1 ) { throw new WriteException ( 'Batch insert error: ' . $ result [ 'err' ] ) ; } } elseif ( false === $ result ) { throw new WriteException ( 'Batch insert error' ) ; } } return $ this ; }
Copy selected documents to another collection

@param string $targetCollectionName
@param string|null $targetDatabaseName Target database name. If not specified - use current
@param int $batchLimit count of documents to get from old and insert to new collection per time

@return Cursor

@throws WriteException
Copy selected documents to another collection