ent

ent

  • Docs
  • GoDoc
  • Github
  • Blog

›Code Generation

Getting Started

  • Quick Introduction

Schema

  • Introduction
  • Fields
  • Edges
  • Indexes
  • Mixin
  • Annotations

Code Generation

  • Introduction
  • CRUD API
  • Graph Traversal
  • Eager Loading
  • Hooks
  • Privacy
  • Transactions
  • Predicates
  • Aggregation
  • Paging And Ordering

Migration

  • Database Migration
  • Supported Dialects

Misc

  • External Templates
  • GraphQL Integration
  • sql.DB Integration
  • Testing
  • FAQ
  • Feature Flags

Aggregation

Group By

Group by name and age fields of all users, and sum their total age.

package main

import (
    "context"
    
    "<project>/ent"
    "<project>/ent/user"
)

func Do(ctx context.Context, client *ent.Client) {
    var v []struct {
        Name  string `json:"name"`
        Age   int    `json:"age"`
        Sum   int    `json:"sum"`
        Count int    `json:"count"`
    }
    err := client.User.Query().
        GroupBy(user.FieldName, user.FieldAge).
        Aggregate(ent.Count(), ent.Sum(user.FieldAge)).
        Scan(ctx, &v)
}

Group by one field.

package main

import (
    "context"
    
    "<project>/ent"
    "<project>/ent/user"
)

func Do(ctx context.Context, client *ent.Client) {
    names, err := client.User.
        Query().
        GroupBy(user.FieldName).
        Strings(ctx)
}
← PredicatesPaging And Ordering →
  • Group By
Docs
Getting StartedSchema GuideCode GenerationSchema Migration
Legal
PrivacyTerms
Credit
The Go gopher was designed by Renee French. The design is licensed under the Creative Commons 3.0 Attributions license. Read this article for more details.

Design by Moriah Rich, illustration by Ariel Mashraki.
Facebook Open Source
Copyright 2021 Facebook Inc.