Is it possible to get a string representation of a `Ref` in a FQL query?

In FQL, is it possible to get a string out of a Ref?

 Ref(Collection("rob_tyson_evan"), "12345") 
 // ==> "Ref(Collection(\"rob_tyson_evan\"), \"12345\")"

Using ToString won’t work:

ToString(Ref(Collection("rob_tyson_evan"), "12345")
// => Error: "Cannot cast Ref to String."

Hi @da0099,

Can I ask why you’re trying to cast a Ref as a string in the first place? More details around the use case might help us come up with a potential solution for you.


I was asking out of curiosity. I thought, from a usability standpoint, it would make sense ToString would return a Ref string representation. Since that is not the case, I was curious as to why it doesn’t. Security reasons or to avoid bad coding practices, maybe?

A Reference is a compound object, containing a reference to a collection, and a document id within that collection. There is no string representation that you can use in a query in place of a reference object.

That said, it is often a requirement to serialize references for use in web-based UIs, so converting a reference to a string could be a useful serialization.

You could certainly do something like this:

    ref: Ref(Collection('Letters'), '101'),
    collection: Select('collection', Var('ref')),
    cname: Select('id', Var('collection')),
    docid: Select('id', Var('ref')),
  Concat([Var('cname'), Var('docid')], ' ')

which outputs:

Letters 101

I used Let so that you could see the technique for extracting the elements from the reference.

Once you have that string, to use it in a query you would need to create a valid reference. You would need to split on the space character, and then:

Ref(Collection(collection_name), document_id)

Which assumes that collection_name and document_id are variables in your host language. You didn’t indicate which driver you’re using, so I’m not providing those specific details.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.