yii\mongodb\Query::scalar PHP Method

scalar() public method

The value returned will be the first column in the first row of the query results. Column _id will be automatically excluded from select fields, if [[select]] is not empty and _id is not selected explicitly.
Since: 2.1.2
public scalar ( Connection $db = null ) : string | null | false
$db Connection the MongoDB connection used to generate the query. If this parameter is not given, the `mongodb` application component will be used.
return string | null | false the value of the first column in the first row of the query result. `false` is returned if the query result is empty.
    public function scalar($db = null)
    {
        $originSelect = (array) $this->select;
        if (!isset($originSelect['_id']) && array_search('_id', $originSelect, true) === false) {
            $this->select['_id'] = false;
        }
        $cursor = $this->buildCursor($db);
        $row = $this->fetchRows($cursor, false);
        if (empty($row)) {
            return false;
        }
        return reset($row);
    }