xp-forge / mongodb

MongoDB for the XP Framework
0 stars 0 forks source link

Implement multi-document transactions #13

Closed thekid closed 2 years ago

thekid commented 2 years ago

Implements #12:

use com\mongodb\MongoConnection;

$c= new MongoConnection('mongodb+srv://server.example.com');

$transaction= $c->session()->transaction();

// Update two documents
$collection->update($id1, ['$inc' => ['qty' => -1]], $transaction);
$collection->update($id2, ['$inc' => ['qty' => 1]], $transaction);

// Reads changed values
Console::writeLine('Inside: ', [...$collection->find(['qty' => ['$exists' => true]], $transaction)]);

$transaction->commit();  // or $transaction->abort();

// Reads committed values, original values if aborted
Console::writeLine('Outside: ', [...$collection->find(['qty' => ['$exists' => true]])]);