MongoCollection::remove PHP Method

remove() public method

Remove records from this collection
public remove ( array $criteria = [], array $options = [] ) : boolean | array
$criteria array Query criteria for the documents to delete.
$options array An array of options for the remove operation.
return boolean | array Returns an array containing the status of the removal if the "w" option is set. Otherwise, returns TRUE.
    public function remove(array $criteria = [], array $options = [])
    {
        $multiple = isset($options['justOne']) ? !$options['justOne'] : true;
        $method = $multiple ? 'deleteMany' : 'deleteOne';
        try {
            /** @var \MongoDB\DeleteResult $result */
            $result = $this->collection->{$method}(TypeConverter::fromLegacy($criteria), $this->convertWriteConcernOptions($options));
        } catch (\MongoDB\Driver\Exception\Exception $e) {
            throw ExceptionConverter::toLegacy($e);
        }
        if (!$result->isAcknowledged()) {
            return true;
        }
        return ['ok' => 1.0, 'n' => $result->getDeletedCount(), 'err' => null, 'errmsg' => null];
    }

Usage Example

Beispiel #1
0
 public function deleteExceedingQuota($quota)
 {
     $threshold = 0.9 * $quota;
     $currentSize = $this->getPictureTotalSize();
     $counter = 0;
     if ($currentSize > $threshold) {
         $toPurge = $currentSize - $threshold;
         // cleaning
         $cursor = $this->collection->find(['-class' => 'picture'], ['storageKey' => true, 'size' => true])->sort(['_id' => 1]);
         // starting from older pictures
         $sum = 0;
         foreach ($cursor as $item) {
             if ($sum < $toPurge) {
                 $this->storage->remove($item['storageKey']);
                 $this->collection->remove(['_id' => $item['_id']]);
                 $sum += $item['size'];
                 $counter++;
             } else {
                 // when we have deleted enough old pictures to reach the exceeding size to purge
                 break;
             }
         }
     }
     return $counter;
 }
All Usage Examples Of MongoCollection::remove