xp-forge / mongodb

MongoDB for the XP Framework
0 stars 0 forks source link

Add Collection::run() to run commands #21

Closed thekid closed 2 years ago

thekid commented 2 years ago

This new method replaces Collection::command() and handles cursor results transparently, guaranteeing to not leave any unfinished cursors hanging around.

Old code

$list= $collection->command('listIndexes, []);
foreach ($list['cursor']['firstBatch'] as $index) {
  // Work with index
}

if ($list['cursor']['id']->number() > 0) {
  // Handle next batch, either by fetching or by killing the cursor
}

New code

foreach ($collection->run('listIndexes, [])->cursor() as $index) {
  // Work with index
}

Generic handling

$result= $collection->run($name, (array)$arguments);
if ($cursor= $result->cursor()) {
  foreach ($cursor as $i => $document) {
    Console::writeLine($i, ': ', $document);
  }
} else {
  Console::writeLine($result->value());
}
thekid commented 2 years ago

Released in https://github.com/xp-forge/mongodb/releases/tag/v1.4.0

thekid commented 1 year ago

The deprecated command() method was removed in Released in https://github.com/xp-forge/mongodb/releases/tag/v2.0.0