-
Type:
Task
-
Resolution: Done
-
Priority:
Major - P3
-
Affects Version/s: None
-
Component/s: None
-
None
-
None
-
None
-
None
-
None
-
None
-
None
The spec states that bulkWrite() take a required requests parameter (array of write models), followed by write options. Since we're abstracting libmongoc, it makes the most sense for our requests parameter to be a WriteBatch object from Phongo.
If we make WriteBatch's methods fluent, we could allow users to do:
$collection->bulkWrite( (new WriteBatch(true)) /* ordered */ ->insert(['x' => 1]) ->update(['y' => 2], ['$inc' => ['y' => 1]]) ->remove(['z' => 3]) );
However, I believe this conflicts with our original plan for WriteBatch::insert() to return the generated ObjectId.
To be compliant with the spec, we should also support a literal syntax using arrays (write model objects would be needlessly expensive), similar to the JavaScript implementation:
$collection->bulkWrite(
// Required writes param (an array of operations)
[
// Like explain(), operations identified by single key
[
'insertOne' => [
['x' => 1]
],
],
[
'updateMany' => [
['x' => 1],
['$set' => ['x' => 2]],
],
],
[
'updateOne' => [
['x' => 3],
['$set' => ['x' => 4]],
// Optional params are still permitted
['upsert' => true],
],
],
[
'deleteOne' => [
['x' => 1],
],
],
[
'deleteMany' => [
// Required arguments must still be specified
[],
],
],
],
// Optional named params in an associative array
['ordered' => false]
);