Take the following code:
app('db')->connection('mongodb')->table($collectionName)
->where($key, $value)->value($propertyName);
If using mysql, it runs a query like:
SELECT $propertyName FROM $collectionName WHERE $key = $value LIMIT 1
And it returns the value of $propertyName.
See https://github.com/illuminate/database/blob/8b600637ff7f472bc41705b5a7dabe9501eebe56/Query/Builder.php#L2027
However, due to the fact that mongo always returns the _id
, whenever you use a projection, this method always returns _id
, instead of $propertyName
This method should be overriden with something like:
public function value($column)
{
$result = (array) $this->first([$column]);
return $result[$column] ?? null;
}