Uploaded image for project: 'PHP Driver: Library'
  1. PHP Driver: Library
  2. PHPLIB-10

Implement Collection::bulkWrite()

    • Type: Icon: Task Task
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 0.1.0
    • Affects Version/s: None
    • Component/s: 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]
      );
      

            Assignee:
            bjori Hannes Magnusson
            Reporter:
            jmikola@mongodb.com Jeremy Mikola
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: