You are here

public function KeyvalueMongodb::setIfNotExists in MongoDB 8

Saves a value for a given key if it does not exist yet.

Parameters

string $key: The key of the data to store.

mixed $value: The data to store.

Return value

bool TRUE if the data was set, FALSE if it already existed.

Overrides KeyValueStoreInterface::setIfNotExists

File

src/KeyvalueMongodb.php, line 191

Class

KeyvalueMongodb
This class holds a MongoDB key-value backend.

Namespace

Drupal\mongodb

Code

public function setIfNotExists($key, $value) {
  try {
    return $this
      ->collection()
      ->insert($this
      ->getObject($key, $value));
  } catch (\MongoCursorException $e) {
    return FALSE;
  }
}