FatEcto v0.5.0 FatEcto.FatQuery.FatAggregate View Source

Builds an aggregate query with an aggregate method passed in the params as a string or list.

=> $sum/$avg

Parameters

  • queryable - Ecto Queryable that represents your schema name, table name or query.
  • query_opts - Aggregate query options as a map.

Examples

iex> query_opts = %{
...>  "$aggregate" => %{"$sum" => "total_staff", "$avg" => "rating"},
...>  "$where" => %{"rating" => 5},
...>  "$group" => "id" 
...> }   
iex> Elixir.MyApp.Query.build!(FatEcto.FatHospital, query_opts)
#Ecto.Query<from f0 in FatEcto.FatHospital, where: f0.rating == ^5 and ^true, group_by: [f0.id], select: merge(merge(f0, %{"$aggregate" => %{"$avg": %{^"rating" => avg(f0.rating)}}}), %{"$aggregate" => %{"$sum": %{^"total_staff" => sum(f0.total_staff)}}})>

Options

  • $aggregate- Specify the type of aggregate method/methods to apply.
  • $where - Added the where attribute in the query.
  • $group - Group the records with a specific field.

=> $min/$max

Parameters

  • queryable - Ecto Queryable that represents your schema name, table name or query.
  • query_opts - Aggregate query options as a map.

Examples

iex> query_opts = %{
...>  "$aggregate" => %{"$min" => "total_staff", "$max" => "rating"},
...>  "$where" => %{"rating" => 5},
...>  "$group" => "id" 
...> }   
iex> Elixir.MyApp.Query.build!(FatEcto.FatHospital, query_opts)
#Ecto.Query<from f0 in FatEcto.FatHospital, where: f0.rating == ^5 and ^true, group_by: [f0.id], select: merge(merge(f0, %{"$aggregate" => %{"$max": %{^"rating" => max(f0.rating)}}}), %{"$aggregate" => %{"$min": %{^"total_staff" => min(f0.total_staff)}}})>

Options

  • $aggregate- Specify the type of aggregate method/methods to apply.
  • $where - Added the where attribute in the query.
  • $group - Group the records with a specific field.

=> $count/$count_distinct

Parameters

  • queryable - Ecto Queryable that represents your schema name, table name or query.
  • query_opts - Aggregate query options as a map.

Examples

iex> query_opts = %{
...>  "$aggregate" => %{"$count" => ["total_staff"], "$count_distinct" => ["rating"]},
...>  "$where" => %{"rating" => 5},
...>  "$group" => "id" 
...> }   
iex> Elixir.MyApp.Query.build!(FatEcto.FatHospital, query_opts)
#Ecto.Query<from f0 in FatEcto.FatHospital, where: f0.rating == ^5 and ^true, group_by: [f0.id], select: merge(merge(f0, %{"$aggregate" => %{"$count": %{^"total_staff" => count(f0.total_staff)}}}), %{"$aggregate" => %{"$count_distinct": %{^"rating" => count(f0.rating, :distinct)}}})>

Options

  • $aggregate - Specify the type of aggregate method/methods to apply.
  • $where - Added the where attribute in the query.
  • $group - Group the records with a specific field.

Link to this section Summary

Link to this section Functions

Link to this function

build_aggregate(queryable, aggregate_params, options)

View Source

Builds an aggregate query.

Parameters

  • queryable - Ecto Queryable that represents your schema name, table name or query.
  • aggregate_params - Aggregate query options as a map.
  • options - Pass options related to otp_app.

Examples

iex> query_opts = %{
...>  "$aggregate" => %{"$count" => ["total_staff"], "$count_distinct" => ["rating"]},
...>  "$where" => %{"rating" => 5},
...>  "$group" => "id" 
...> }   
iex> Elixir.FatEcto.FatQuery.FatAggregate.build_aggregate(FatEcto.FatHospital, query_opts["$aggregate"], [])
#Ecto.Query<from f0 in FatEcto.FatHospital, select: merge(merge(f0, %{"$aggregate" => %{"$count": %{^"total_staff" => count(f0.total_staff)}}}), %{"$aggregate" => %{"$count_distinct": %{^"rating" => count(f0.rating, :distinct)}}})>