set.distinct()

Get the unique elements of a Set.

Signature

distinct() => Set<A>

Description

Gets the unique elements of the calling Set.

The calling Set isn’t changed.

Eager loading

This method uses eager loading and requires a read of each document in the calling Set. For large Sets, this may result in poor performance and high costs.

Avoid use on large Sets

Avoid using distinct() on large or unbounded Sets that contain 16,000 or more documents.

If a Set contains 16,000 or more documents, the query requires pagination. array.distinct() would only be able to extract unique elements from each page of results.

Instead, retrieve all field values and process them on the client side. See Get unique field values.

Parameters

None

Return value

Type Description

Set<Generic>

Unique elements in the Set.

Examples

Basic example

// `toSet()` converts an Array to a Set.
let set = [1, 1, 2, 3, 3].toSet()
set.distinct()
{
  data: [
    1,
    2,
    3
  ]
}

Get unique products ordered by a customer

In this example, you’ll use distinct() to get a Set of unique products ordered by a specific customer.

The example uses the Order collection. Order collection documents have the following structure:

{
  id: "12345",
  coll: Order,
  ts: Time("2099-07-31T12:42:19Z"),
  // `items` contains a Set of `OrderItem` documents.
  items: {
    data: [
      {
        id: "112233",
        coll: OrderItem,
        ts: Time("2099-07-31T12:42:19Z"),
        order: Order("12345"),
        product: Product("111"),
        quantity: 2
      },
      ...
    ]
  },
  total: 5392,
  status: "cart",
  // `customer` contains a `Customer` document.
  customer: {
    id: "111",
    coll: Customer,
    ts: Time("2099-07-31T12:42:19Z"),
    cart: Order("412483941752112205"),
    // `orders` contains a Set of `OrderItem` documents.
    orders: "hdW...",
    name: "Alice Appleseed",
    email: "alice.appleseed@example.com",
    address: {
      street: "87856 Mendota Court",
      city: "Washington",
      state: "DC",
      postalCode: "20220",
      country: "US"
    }
  },
  createdAt: Time("2099-07-31T12:42:18.774426Z"),
  payment: {}
}

The query:

// Uses the `Customer` collection's `byEmail()` index to
// get `Customer` collection documents by `email` field value.
// In the `Customer` collection, `email` field values are unique
// so return the `first()` (and only) document.
let customer = Customer.byEmail("alice.appleseed@example.com").first()

// Uses the `Order` collection's `byCustomer()` index to
// get `Order` collection documents for the previous customer.
Order.byCustomer(customer)
  // `Order` documents include a `items` field that contains a Set
  // of `OrderItem` documents. Each `OrderItem` document includes a
  // `product` field. This `flatMap()` call extracts the `id` and
  // `name` of each product, and flattens the resulting Set.
  .flatMap(.items.map(.product) { id, name } )
  // Deduplicates the Set of product `id` and `name` values so that
  // it only returns unique elements.
  .distinct()
{
  data: [
    {
      id: "111",
      name: "cups"
    },
    {
      id: "222",
      name: "donkey pinata"
    },
    {
      id: "333",
      name: "pizza"
    }
  ]
}

Is this article helpful? 

Tell Fauna how the article can be improved:
Visit Fauna's forums or email docs@fauna.com

Thank you for your feedback!