You are here

public function MongoCollectionFactory::nextId in MongoDB 8

File

src/MongoCollectionFactory.php, line 197
Definition of Drupal\mongodb\MongodbBundle.

Class

MongoCollectionFactory
Creates mongo collections based on settings.

Namespace

Drupal\mongodb

Code

public function nextId($sequence_id = 'generic', $existing_id = 0) {
  if ($existing_id) {
    $this
      ->get('sequences')
      ->update(array(
      '_id' => $sequence_id,
      'seq' => array(
        '$lt' => $existing_id,
      ),
    ), array(
      '$set' => array(
        'seq' => $existing_id,
      ),
    ));
  }
  $result = $this
    ->get('sequences')
    ->findAndModify(array(
    '_id' => $sequence_id,
  ), array(
    '$inc' => array(
      'seq' => 1,
    ),
  ), NULL, array(
    'upsert' => TRUE,
  ));
  $seq = $result ? $result['seq'] : 0;
  return $seq + 1;
}