Fundamental Query Syntax I'm just not getting


New to Fauna & FQL and looking for the best practice for the following - When I have a document with a reference to another document inside it, what’s the best way to return the that document within the original document instead of just the ref? … and likewise if the nested documents a ref that I need

Below is the doc, if I want to return all the invite docs with sent = false and include the league document, not just the ref, what’s the correct approach? and following on from that, if the league document had a ref to another doc that I wanted to return too


  "ref": ,
  "data": {
    "league": Ref(Collection("leagues"), "id"),
    "expiry": "",
    "email": "",
    "sent": false,
    "accepted": false

Hi jkeane!

You didn’t supply the query that you are already running, but I’m guessing it looks something like this:

Paginate(Match(Index("invites_by_sent"), false))

To substitute the reference with the referenced document, you would need to expand your query like so:

  Paginate(Match(Index("invites_by_sent"), false)),
        invite_doc: Get(Var("invite_ref")), // fetch the full invite document
        league_ref: Select(["data", "league"], Var("invite_doc")), // get the league ref
        league_doc: Get(Var("league_ref")), // fetch the full league document
      // compose the return value for this entry in the result set
        ref: Var("invite_ref"),
        ts: Select(["ts"], Var("invite_doc")),
        data: {
          league:   Var("league_doc"),
          expiry:   Select(["data", "expiry"], Var("invite_doc")),
          email:    Select(["data", "email"], Var("invite_doc")),
          sent:     Select(["data", "sent"], Var("invite_doc")),
          accepted: Select(["data", "accepted"], Var("invite_doc")),

This example works by using Map to iterate over all of the entries in the result set from the Paginate call. Map calls the supplied Lambda function for each result set entry.

Inside the Lambda, Let is used to first gather some working values, namely the full invite document, the league field value, and the full league document from the league reference.

The second parameter to Let is the object we want to return for the current result set item: a new object that mirrors the structure of the invite document, but we use the fetched league_doc value instead of the reference.

Unfortunately, the Merge function doesn’t help us here because it doesn’t merge objects recursively, so we have to mimic the document field by field.

If the league document has a ref that you want to include in the result, then you’d revise the Let definition to fetch that document too, and then make the same kind of mirrored object for the league document.

Thank you very much, apologies regarding the lack of current query but you nailed it!
I didn’t realize I could reference a just created variable in the first parameter object of the Let like you do with league_ref above (duh) - this is the key going forward for me!

1 Like

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