Collection()

Dynamically get the Collection by name.

Signature

Collection(name: String): Any

Description

The Collection() method gets the name collection.

Calling Collection() is similar to accessing <collectionName> directly, except:

  • Collection() returns an Any value

  • <collectionName> is a Collection value

This difference only affects static typing, not runtime behavior. Both Collection() and <collectionName> support the same collection instance methods.

In most cases, you should use <collectionName>. However, Collection() is useful if you need to iterate through a list of collection names.

Parameters

Parameter Type Required Description

name

String

Yes

Collection name

Return value

Type Description

Any

The name collection.

Examples

Collection() returns an Any value. For example:

Collection("Product")

Returns an Any value:

Product

Difference with <collectionName>

Accessing <collectionName> directly returns a similar value, typed as a Collection. For example:

Product

Returns the Product Collection value:

Product

Method chaining

In most cases, you’ll chain other collection instance methods to Collection(). For example:

Collection("Product").create({
  name: "pinata",
  description: "Giant Burrito Pinata",
  price: 23.99,
  quantity: 0,
  store: Store("401610017107607625"),
  backorderLimit: 10,
  backordered: true
})

Returns an Any value:

{
  id: "401610110022975565",
  coll: Product,
  ts: Time("2099-06-24T21:21:37.170Z"),
  name: "pinata",
  description: "Giant Burrito Pinata",
  price: 23.99,
  quantity: 0,
  store: Store("401610017107607625"),
  backorderLimit: 10,
  backordered: true
}

Dynamically specify a collection name

Use Collection() to dynamically specify collection names in a query.

For example, the following user-defined function (UDF) uses Collection() to pass a collection name as an argument:

// Accepts a collection name as an argument.
function getPriceLowtoHigh(collection) {
  // Uses `Collection()` to dynamically specify
  // the collection name.
  Collection(collection).sortedByPriceLowToHigh() {
    price,
    name,
    description
  }
}

The following query calls the function:

// Calls the `getPriceLowtoHigh()` UDF with
// a `Product` collection argument.
getPriceLowtoHigh("Product")

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!