FQL v4 will be decommissioned on June 30, 2025. Ensure that you complete your migration from FQL v4 to FQL v10 by that date.

For more details, review the migration guide. Contact support@fauna.com with any questions.

Document reference

Problem

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

Solution

Use the Select function:

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'}
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>}]
try
{
    Value result = await client.Query(
        Let(
            "doc", Get(Ref(Collection("users"), "1"))
        ).In(
            Obj(
                "document", Var("doc"),
                "reference", Select("ref", Var("doc")),
                "reference collection", Select(
                    Arr("ref", "collection"),
                    Var("doc")
                ),
                "collection name", Select(
                    Arr("ref", "collection", "id"),
                    Var("doc")
                ),
                "document ID", Select(
                    Arr("ref", "id"),
                    Var("doc")
                )
            )
        )
    );
    Console.WriteLine(result);
}
catch (Exception e)
{
    Console.WriteLine($"ERROR: {e.Message}");
}
ObjectV(document: ObjectV(ref: RefV(id = "1", collection = RefV(id = "users", collection = RefV(id = "collections"))),ts: LongV(1668014161170000),data: ObjectV(name: StringV(Alice Crypto),email: StringV(alice@site.example.com))),reference: RefV(id = "1", collection = RefV(id = "users", collection = RefV(id = "collections"))),reference collection: RefV(id = "users", collection = RefV(id = "collections")),collection name: StringV(users),document ID: StringV(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!