Document reference

Problem

You need to extract the collection reference or document ID from a document reference.

Solution

Use the Select function:

Copied!
(
  {
    doc: ((("users"), "1")),
  },
  {
    "document": ("doc"),
    "reference": ("ref", ("doc")),
    "reference collection": (["ref", "collection"], ("doc")),
    "collection name": (["ref", "collection", "id"], ("doc")),
    "document ID": (["ref", "id"], ("doc")),
  }
)
{
  document: {
    ref: (("users"), "1"),
    ts: 1668013995250000,
    data: { name: 'Alice Crypto', email: 'alice@site.example.com' }
  },
  reference: (("users"), "1"),
  'reference collection': ("users"),
  'collection name': 'users',
  'document ID': '1'
}
Query metrics:
  •    bytesIn: 369

  •   bytesOut: 465

  • computeOps:   1

  •    readOps:   1

  •   writeOps:   0

  •  readBytes:  96

  • writeBytes:   0

  •  queryTime: 6ms

  •    retries:   0

Discussion

The example uses the Let function to fetch the document, and then return an object that reports the document itself, the document’s reference, and the components of the document reference.

More information

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!