Documentation

BulkWrite
in package

FinalYes

Operation for executing multiple write operations.

Tags
see
Collection::bulkWrite()
psalm-type

Document = object|array

psalm-type

OperationType = array{deleteMany: array{0: Document, 1?: array}}|array{deleteOne: array{0: Document, 1?: array}}|array{insertOne: array{0: Document}}|array{replaceOne: array{0: Document, 1: Document, 2?: array}}|array{updateMany: array{0: Document, 1: Document, 2?: array}}|array{updateOne: array{0: Document, 1: Document, 2?: array}}

Table of Contents

Constants

DELETE_MANY  = 'deleteMany'
DELETE_ONE  = 'deleteOne'
INSERT_ONE  = 'insertOne'
REPLACE_ONE  = 'replaceOne'
UPDATE_MANY  = 'updateMany'
UPDATE_ONE  = 'updateOne'

Properties

$collectionName  : string
$databaseName  : string
$operations  : array<string|int, mixed>
$options  : array<string|int, mixed>

Methods

__construct()  : mixed
Constructs a bulk write operation.
execute()  : BulkWriteResult
Execute the operation.
createBulkWriteOptions()  : array<string|int, mixed>
Create options for constructing the bulk write.
createExecuteOptions()  : array<string|int, mixed>
Create options for executing the bulk write.
validateOperations()  : array<string|int, mixed>

Constants

DELETE_MANY

public mixed DELETE_MANY = 'deleteMany'

DELETE_ONE

public mixed DELETE_ONE = 'deleteOne'

INSERT_ONE

public mixed INSERT_ONE = 'insertOne'

REPLACE_ONE

public mixed REPLACE_ONE = 'replaceOne'

UPDATE_MANY

public mixed UPDATE_MANY = 'updateMany'

UPDATE_ONE

public mixed UPDATE_ONE = 'updateOne'

Properties

$collectionName

private string $collectionName

$operations

private array<string|int, mixed> $operations
Tags
psalm-var

list<OperationType>

$options

private array<string|int, mixed> $options

Methods

__construct()

Constructs a bulk write operation.

public __construct(string $databaseName, string $collectionName, array<string|int, mixed> $operations[, array<string|int, mixed> $options = [] ]) : mixed

Example array structure for all supported operation types:

[ [ 'deleteMany' => [ $filter, $options ] ], [ 'deleteOne' => [ $filter, $options ] ], [ 'insertOne' => [ $document ] ], [ 'replaceOne' => [ $filter, $replacement, $options ] ], [ 'updateMany' => [ $filter, $update, $options ] ], [ 'updateOne' => [ $filter, $update, $options ] ], ]

Arguments correspond to the respective Operation classes; however, the writeConcern option is specified for the top-level bulk write operation instead of each individual operation.

Supported options for deleteMany and deleteOne operations:

  • collation (document): Collation specification.

Supported options for replaceOne, updateMany, and updateOne operations:

  • collation (document): Collation specification.

  • upsert (boolean): When true, a new document is created if no document matches the query. The default is false.

Supported options for replaceOne and updateOne operations:

  • sort (document): Determines which document the operation modifies if the query selects multiple documents.

    This is not supported for server versions < 8.0 and will result in an exception at execution time if used.

Supported options for updateMany and updateOne operations:

  • arrayFilters (document array): A set of filters specifying to which array elements an update should apply.

Supported options for the bulk write operation:

  • builderEncoder (MongoDB\Codec\Encoder): Encoder for query and aggregation builders. If not given, the default encoder will be used.

  • bypassDocumentValidation (boolean): If true, allows the write to circumvent document level validation. The default is false.

  • codec (MongoDB\Codec\DocumentCodec): Codec used to decode documents from BSON to PHP objects. This option is also used to encode PHP objects into BSON for insertOne and replaceOne operations.

  • comment (mixed): BSON value to attach as a comment to this command(s) associated with this bulk write.

    This is not supported for servers versions < 4.4.

  • ordered (boolean): If true, when an insert fails, return without performing the remaining writes. If false, when a write fails, continue with the remaining writes, if any. The default is true.

  • let (document): Map of parameter names and values. Values must be constant or closed expressions that do not reference document fields. Parameters can then be accessed as variables in an aggregate expression context (e.g. "$$var").

  • session (MongoDB\Driver\Session): Client session.

  • writeConcern (MongoDB\Driver\WriteConcern): Write concern.

Parameters
$databaseName : string

Database name

$collectionName : string

Collection name

$operations : array<string|int, mixed>

List of write operations

$options : array<string|int, mixed> = []

Command options

Tags
psalm-param

list<OperationType> $operations

throws
InvalidArgumentException

for parameter/option parsing errors

validateOperations()

private validateOperations(array<string|int, mixed> $operations, DocumentCodec|null $codec, Encoder $builderEncoder) : array<string|int, mixed>
Parameters
$operations : array<string|int, mixed>
$codec : DocumentCodec|null
$builderEncoder : Encoder
Tags
psalm-param

list<OperationType> $operations

psalm-return

list<OperationType>

Return values
array<string|int, mixed>

        
On this page

Search results