Doctrine\MongoDB\Query\Expr::addToSet PHP Method

addToSet() public method

If the field does not exist, it will be set to an array containing the unique value(s) in the argument. If the field is not an array, the query will yield an error. Multiple values may be specified by provided an Expr object and using {@link Expr::each()}.
See also: Builder::addToSet()
See also: http://docs.mongodb.org/manual/reference/operator/addToSet/
See also: http://docs.mongodb.org/manual/reference/operator/each/
public addToSet ( mixed | Expr $valueOrExpression )
$valueOrExpression mixed | Expr
    public function addToSet($valueOrExpression)
    {
        if ($valueOrExpression instanceof Expr) {
            $valueOrExpression = $valueOrExpression->getQuery();
        }
        $this->requiresCurrentField();
        $this->newObj['$addToSet'][$this->currentField] = $valueOrExpression;
        return $this;
    }

Usage Example

示例#1
0
 /**
  * Append one or more values to the current array field only if they do not
  * already exist in the array.
  *
  * If the field does not exist, it will be set to an array containing the
  * unique value(s) in the argument. If the field is not an array, the query
  * will yield an error.
  *
  * Multiple values may be specified by provided an Expr object and using
  * {@link Expr::each()}.
  *
  * @see Expr::addToSet()
  * @see http://docs.mongodb.org/manual/reference/operator/addToSet/
  * @see http://docs.mongodb.org/manual/reference/operator/each/
  * @param mixed|Expr $valueOrExpression
  * @return self
  */
 public function addToSet($valueOrExpression)
 {
     $this->expr->addToSet($valueOrExpression);
     return $this;
 }