Document reference

Problem

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

Solution

Use the Select function:

result, err := client.Query(
	f.Let().Bind(
		"doc", f.Get(f.Ref(f.Collection("users"), "1")),
	).In(
		f.Obj{
			"document": f.Var("doc"),
			"reference": f.Select("ref", f.Var("doc")),
			"reference collection": f.Select(
				f.Arr{"ref", "collection"},
				f.Var("doc"),
			),
			"collection name": f.Select(
				f.Arr{"ref", "collection", "id"},
				f.Var("doc"),
			),
			"document ID": f.Select(
				f.Arr{"ref", "id"},
				f.Var("doc"),
			),
		},
	))

if err != nil {
	fmt.Fprintln(os.Stderr, err)
} else {
	fmt.Println(result)
}
map[collection name:users document:map[data:map[email:alice@site.example.com name:Alice Crypto] ref:{1 0x1400011dec0 0x1400011dec0 <nil>} ts:1668014775490000] document ID:1 reference:{1 0x140001a80c0 0x140001a80c0 <nil>} reference collection:{users 0x140001a81b0 0x140001a81b0 <nil>}]
client.query(
  q.Let(
    {
      doc: q.Get(q.Ref(q.Collection('users'), '1')),
    },
    {
      document: q.Var('doc'),
      reference: q.Select('ref', q.Var('doc')),
      'reference collection': q.Select(
        ['ref', 'collection'],
        q.Var('doc')
      ),
      'collection name': q.Select(
        ['ref', 'collection', 'id'],
        q.Var('doc')
      ),
      'document ID': q.Select(['ref', 'id'], q.Var('doc')),
    }
  )
)
.then((res) => console.log(res))
.catch((err) => console.error(
  'Error: [%s] %s: %s',
  err.name,
  err.message,
  err.errors()[0].description,
))
{
  document: {
    ref: Ref(Collection("users"), "1"),
    ts: 1668013992680000,
    data: { name: 'Alice Crypto', email: 'alice@site.example.com' }
  },
  reference: Ref(Collection("users"), "1"),
  'reference collection': Collection("users"),
  'collection name': 'users',
  'document ID': '1'
}
result = client.query(
  q.let(
    {
      "doc": q.get(q.ref(q.collection("users"), "1")),
    },
    {
      "document": q.var("doc"),
      "reference": q.select("ref", q.var("doc")),
      "reference collection": q.select(["ref", "collection"], q.var("doc")),
      "collection name": q.select(["ref", "collection", "id"], q.var("doc")),
      "document ID": q.select(["ref", "id"], q.var("doc")),
    }
  )
)
print(result)
{'document': {'ref': Ref(id=1, collection=Ref(id=users, collection=Ref(id=collections))), 'ts': 1668014292890000, 'data': {'name': 'Alice Crypto', 'email': 'alice@site.example.com'}}, 'reference': Ref(id=1, collection=Ref(id=users, collection=Ref(id=collections))), 'reference collection': Ref(id=users, collection=Ref(id=collections)), 'collection name': 'users', 'document ID': '1'}
Let(
  {
    doc: Get(Ref(Collection("users"), "1")),
  },
  {
    "document": Var("doc"),
    "reference": Select("ref", Var("doc")),
    "reference collection": Select(["ref", "collection"], Var("doc")),
    "collection name": Select(["ref", "collection", "id"], Var("doc")),
    "document ID": Select(["ref", "id"], Var("doc")),
  }
)
{
  document: {
    ref: Ref(Collection("users"), "1"),
    ts: 1668013995250000,
    data: { name: 'Alice Crypto', email: 'alice@site.example.com' }
  },
  reference: Ref(Collection("users"), "1"),
  'reference collection': 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.

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!