lithium\data\source\MongoDb::delete()

public method

Delete document

Parameters

  • string $query
  • array $options

Returns

boolean

Filter

This method can be filtered.

Source

	public function delete($query, array $options = []) {
		$defaults = [
			'ordered' => true,
			'justOne' => false,
			'w' => $this->_config['uriOptions']['w'],
			'wTimeoutMS' => $this->_config['uriOptions']['wTimeoutMS'],
			'journal' => $this->_config['uriOptions']['journal']
		];
		$options += $defaults;
		$params = compact('query', 'options');

		return Filters::run($this, __FUNCTION__, $params, function($params) {
			$query = $params['query'];
			$options = $params['options'];
			$args = $query->export($this, ['keys' => ['source', 'conditions']]);
			$source = $args['source'];
			$conditions = $args['conditions'];

			$deleteQuery = new BulkWrite(['ordered' => $options['ordered']]);
			$deleteQuery->delete($conditions, ['limit' => $options['justOne']]);

			try {
				$writeConcern = new WriteConcern($options['w'], $options['wTimeoutMS'], $options['journal']);
				$this->manager->executeBulkWrite("{$this->_config['database']}.{$source}", $deleteQuery, $writeConcern);
				if ($query->entity()) {
					$query->entity()->sync(null, [], ['dematerialize' => true]);
				}
				return true;
			} catch (BulkWriteException $e) {
				return false;
			}
		});
	}