/** * Used to get hash that uniquely identifies current query */ public function getHash() { $hash = array(); // expression $hash[] = json_encode($this->expression->toArray()); // sorts if ($this->sort) { $sort = $this->sort; ksort($sort); $hash[] = implode('', array_merge(array_keys($sort), array_values($sort))); } // fields if ($this->fields) { $fields = $this->fields; ksort($fields); $hash[] = implode('', array_merge(array_keys($fields), array_values($fields))); } // skip and limit $hash[] = $this->skip; $hash[] = $this->limit; // get hash return md5(implode(':', $hash)); }
public function merge(Expression $expression) { $this->_expression = array_merge_recursive($this->_expression, $expression->toArray()); return $this; }
/** * Update multiple documents * * @param \Sokil\Mongo\Expression|array|callable $expression expression to define * which documents will change. * @param \Sokil\Mongo\Operator|array|callable $updateData new data or operators to update * @param array $options update options, see http://php.net/manual/ru/mongocollection.update.php * @return \Sokil\Mongo\Collection * @throws \Sokil\Mongo\Exception */ public function update($expression, $updateData, array $options = array()) { // execute update operator $result = $this->getMongoCollection()->update(Expression::convertToArray($expression), Operator::convertToArray($updateData), $options); // if write concern acknowledged if (is_array($result)) { if ($result['ok'] != 1) { throw new Exception(sprintf('Update error: %s: %s', $result['err'], $result['errmsg'])); } return $this; } // if write concern unacknowledged if (!$result) { throw new Exception('Update error'); } return $this; }
public function testPipeline_MatchExpression() { $pipeline = new Pipeline($this->collection); $expression = new Expression(); $expression->where('a', 1)->whereLess('b', 12); $pipeline->match($expression); $this->assertEquals('[{"$match":{"a":1,"b":{"$lt":12}}}]', (string) $pipeline); }