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

public method

Update document

Parameters

  • string $query
  • array $options

Returns

boolean

Filter

This method can be filtered.

Source

	public function update($query, array $options = []) {
		$defaults = [
			'ordered' => true,
			'multiple' => true,
			'upsert' => 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) {
			$exporter = $this->_classes['exporter'];

			$options = $params['options'];
			$query = $params['query'];
			$args = $query->export($this, ['keys' => ['conditions', 'source', 'data']]);
			$source = $args['source'];
			$data = $args['data'];

			if ($query->entity()) {
				$data = $exporter::get('update', $data);
			}

			$update = $query->entity() ? $exporter::toCommand($data) : $data;

			if (empty($update)) {
				return true;
			}
			if ($options['multiple'] && !preg_grep('/^\$/', array_keys($update))) {
				$update = ['$set' => $update];
			}

			$updateQuery = new BulkWrite(['ordered' => $options['ordered']]);
			$updateQuery->update($args['conditions'], $update, [
				'multi' => $options['multiple'],
				'upsert' => $options['upsert']
			]);

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