Skip to content

Instantly share code, notes, and snippets.

@PierBover
Created May 8, 2020 15:55
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PierBover/70652ded623c6a76db1a0a6fb938a09f to your computer and use it in GitHub Desktop.
Save PierBover/70652ded623c6a76db1a0a6fb938a09f to your computer and use it in GitHub Desktop.
FQL recursive function to get nested documents

This is how you'd call the function:

Call(Function("GetNestedDocument"), [Ref(Collection("SomeCollection"), "264260207986606612"), 0, 3])

Parameters:

  • A Ref of a document.
  • Current depth. Since AFAIK we cannot have default values in Lambda we need to pass 0 for the first level.
  • Max depth. The max number of levels the recursive function will go.
CreateFunction({
name: "GetNestedDocument",
body: Query(
Lambda(
["documentRef", "depth", "maxDepth"],
Let(
{
document: Get(Var("documentRef")),
documentDataArray: ToArray(Select(["data"], Var("document"))),
nextDepth: Add(Var("depth"), 1)
},
{
ref: Select(["ref"],Var("document")),
ts: Select(["ts"],Var("document")),
data: ToObject(
Map(
Var("documentDataArray"),
Lambda(
["key","value"],
If(
And(
IsRef(Var("value")),
LTE(Var("nextDepth"),Var("maxDepth"))
),
[
Var("key"),
Call(Function("GetNestedDocument"), [Var("value"), Var("nextDepth") ,Var("maxDepth")])
],
[Var("key"), Var("value")]
)
)
)
)
}
)
)
)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment