public function KeyValueStore::setIfNotExists in MongoDB 8.2
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
1 call to KeyValueStore::setIfNotExists()
- KeyValueStore::rename in modules/
mongodb_storage/ src/ KeyValueStore.php - Renames a key.
File
- modules/
mongodb_storage/ src/ KeyValueStore.php, line 222
Class
- KeyValueStore
- Class KeyValueStore provides a KeyValueStore as a MongoDB collection.
Namespace
Drupal\mongodb_storageCode
public function setIfNotExists($key, $value) {
$selector = [
'_id' => $this
->stringifyKey($key),
];
$replacement = $selector + [
'value' => serialize($value),
];
$options = [
'upsert' => FALSE,
];
$updateResult = $this->mongoDbCollection
->replaceOne($selector, $replacement, $options);
$result = $updateResult
->getModifiedCount() ? TRUE : FALSE;
return $result;
}