Since: 2.1.0

final class BulkWriteCommand implements Countable (View source)

MongoDB\Driver\BulkWriteCommand collects one or more write operations that should be sent to the server using the ยป bulkWrite command introduced in MongoDB 8.0. After adding any number of insert, update, and delete operations, the command may be executed via MongoDB\Driver\Manager::executeBulkWriteCommand().

Unlike MongoDB\Driver\BulkWrite, where all write operations must target the same collection, each write operation within MongoDB\Driver\BulkWriteCommand may target a different collection.

Write operations may either be ordered (default) or unordered. Ordered write operations are sent to the server, in the order provided, for serial execution. If a write fails, any remaining operations will be aborted. Unordered operations are sent to the server in an arbitrary order where they may be executed in parallel. Any errors that occur are reported after all operations have been attempted.

Methods

__construct(array|null $options = null)

Create a new BulkWriteCommand

int
count()

Count number of write operations in the BulkWriteCommand

void
deleteOne(string $namespace, array|object $filter, array|null $options = null)

Add a deleteOne operation

void
deleteMany(string $namespace, array|object $filter, array|null $options = null)

Add a deleteMany operation

mixed
insertOne(string $namespace, array|object $document)

Add an insertOne operation

void
replaceOne(string $namespace, array|object $filter, array|object $replacement, array|null $options = null)

Add a replaceOne operation

void
updateOne(string $namespace, array|object $filter, array|object $update, array|null $options = null)

Add an updateMany operation

void
updateMany(string $namespace, array|object $filter, array|object $update, array|null $options = null)

Add an updateMany operation

Details

final __construct(array|null $options = null)

Create a new BulkWriteCommand

Parameters

array|null $options

Exceptions

InvalidArgumentException

int count()

Count number of write operations in the BulkWriteCommand

Return Value

int

The custom count as an integer.

The return value is cast to an integer.

final void deleteOne(string $namespace, array|object $filter, array|null $options = null)

Add a deleteOne operation

Parameters

string $namespace

A fully qualified namespace (e.g. "databaseName.collectionName").

array|object $filter

The query predicate. An empty predicate will match all documents in the collection.

array|null $options

Return Value

void

Exceptions

InvalidArgumentException

final void deleteMany(string $namespace, array|object $filter, array|null $options = null)

Add a deleteMany operation

Parameters

string $namespace

A fully qualified namespace (e.g. "databaseName.collectionName").

array|object $filter

The query predicate. An empty predicate will match all documents in the collection.

array|null $options

Return Value

void

Exceptions

InvalidArgumentException

final mixed insertOne(string $namespace, array|object $document)

Add an insertOne operation

Parameters

string $namespace

A fully qualified namespace (e.g. "databaseName.collectionName").

array|object $document

A document to insert.

Return Value

mixed

Returns the _id of the inserted document. If the document did not have an _id, the MongoDB\BSON\ObjectId generated for the insert will be returned.

Exceptions

InvalidArgumentException

final void replaceOne(string $namespace, array|object $filter, array|object $replacement, array|null $options = null)

Add a replaceOne operation

Parameters

string $namespace

A fully qualified namespace (e.g. "databaseName.collectionName").

array|object $filter

The query predicate. An empty predicate will match all documents in the collection.

array|object $replacement

A replacement document.

array|null $options

Return Value

void

Exceptions

InvalidArgumentException

final void updateOne(string $namespace, array|object $filter, array|object $update, array|null $options = null)

Add an updateMany operation

Parameters

string $namespace

A fully qualified namespace (e.g. "databaseName.collectionName").

array|object $filter

The query predicate. An empty predicate will match all documents in the collection.

array|object $update

A document containing either update operators (e.g. $set) or an aggregation pipeline.

array|null $options

Return Value

void

Exceptions

InvalidArgumentException

final void updateMany(string $namespace, array|object $filter, array|object $update, array|null $options = null)

Add an updateMany operation

Parameters

string $namespace

A fully qualified namespace (e.g. "databaseName.collectionName").

array|object $filter

The query predicate. An empty predicate will match all documents in the collection.

array|object $update

A document containing either update operators (e.g. $set) or an aggregation pipeline.

array|null $options

Return Value

void

Exceptions

InvalidArgumentException