Get _just one_ value from Ecto query
With the data structure you use in select
's expression argument you can specify what type of data structure the query will return for a row. I've used []
and %{}
and %Something{}
but you can also specify that each row is just one value without using a data structure at all.
Combine that with Repo.one
to just return one row and you can just get the specific value you are looking for without any destructuring.
age =
User
|> where(id: 42)
|> select([u], u.age)
|> Repo.one()
Tweet