Document reference

Problem

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

Solution

Use the Select function:

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")
                ),
                "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(1625505840300000),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")),document ID: StringV(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"),
			),
			"document ID": f.Select(
				f.Arr{"ref", "id"},
				f.Var("doc"),
			),
		},
	))

if err != nil {
	fmt.Fprintln(os.Stderr, err)
} else {
	fmt.Println(result)
}
map[document:map[data:map[email:alice@site.example.com name:Alice Crypto] ref:{1 0xc0000925a0 0xc0000925a0 <nil>} ts:1625506145900000] document ID:1 reference:{1 0xc000092780 0xc000092780 <nil>} reference collection:{users 0xc000092870 0xc000092870 <nil>}]
System.out.println(
    client.query(
        Let(
            "doc", Get(Ref(Collection("users"), "1"))
        ).in(
            Obj(
                "document", Var("doc"),
                "reference", Select(Value("ref"), Var("doc")),
                "reference collection", Select(
                    Arr(Value("ref"), Value("collection")),
                    Var("doc")
                ),
                "document ID", Select(
                    Arr(Value("ref"), Value("id")),
                    Var("doc")
                )
            )
        )
    ).get());
{document: {ref: ref(id = "1", collection = ref(id = "users", collection = ref(id = "collections"))), ts: 1625506508690000, 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")), document ID: "1"}
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")),
      "document ID": q.Select(["ref", "id"], q.Var("doc")),
    }
  )
)
.then((res) => console.log(res))
.catch((err) => console.log(err))
{
  document: {
    ref: Ref(Collection("users"), "1"),
    ts: 1625505428470000,
    data: { name: 'Alice Crypto', email: 'alice@site.example.com' }
  },
  reference: Ref(Collection("users"), "1"),
  'reference collection': Collection("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")),
      "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': 1625505236270000, '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)), 'document ID': '1'}
try {
  println(Await.result(
  client.query(
    Let(
      Seq(
        "doc" -> Get(Ref(Collection("users"), "1"))
      ),
      Obj(
        "document" -> Var("doc"),
        "reference" -> Select("ref", Var("doc")),
        "reference collection" -> Select(
          Arr("ref", "collection"),
          Var("doc")
        ),
        "document ID" -> Select(
          Arr("ref", "id"),
          Var("doc")
        )
      )
    )
  ),
  5.seconds
))} catch {
  case unknown: Throwable => println("Error: " + unknown.getMessage())
}
{document: {ref: ref(id = "1", collection = ref(id = "users", collection = ref(id = "collections"))), ts: 1625506644100000, 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")), 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")),
    "document ID": Select(["ref", "id"], Var("doc")),
  }
)
{
  document: {
    ref: Ref(Collection("users"), "1"),
    ts: 1625504819720000,
    data: { name: 'Alice Crypto', email: 'alice@site.example.com' }
  },
  reference: Ref(Collection("users"), "1"),
  'reference collection': Collection("users"),
  'document ID': '1'
}

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

Was this article helpful?

We're sorry to hear that.
Tell us how we can improve!
Visit Fauna's Discourse forums or email docs@fauna.com

Thank you for your feedback!