I have following data structure of a single document in Fauna:
"data": {
"title": "Title1",
"blocks": [
{
"block_1": {
"text": "Text1",
"refs": Ref(Collection("xyz"), "XYZ")
},
{
"block_2": {
"text": "Text2",
"refs": Ref(Collection("xyz"), "XYZ2")
}
]
}
Say that the document is given. Is there a way in FQL to get the “blocks” based on their “refs” value? E.g. only return “block_1” based on “refs” = XYZ. I.e. the above example should only return “block_1”.
I hope the question is clear. Feel free to ask for clarification. :slight_smile:
Thanks for your help.
Let({
blocks: Select(["data", "blocks"], Get(Ref(Collection("Blocks"), "299664869783765505"))),
entitiesArray: Map(Var("blocks"), block => ToArray(block)),
entities: Reduce((acc,value) => Append(acc, value) ,[],Var("entitiesArray")),
find: Filter(Var("entities"), entity => Equals(Select([1, "refs"], entity), Ref(Collection("xyz"), "1")))
},
ToObject(Var("find"))
)
But I would suggest revising the blocks
structure. Try to keep it as an array
"blocks": [
{"text": "Text1", "refs": Ref(Collection("xyz"), 1) },
{"text": "Text2", "refs": Ref(Collection("xyz"), 2) }
]
So FQL would be
Let({
blocks: Select(["data", "blocks"], Get(Ref(Collection("Blocks"), "299664869783765505"))),
find: Filter(Var("blocks"), entity => Equals(Select(["refs"], entity), Ref(Collection("xyz"), "1")))
},
Var("find")
)
Or an object
"blocks": {
"block_1": {
"text": "Text1",
"refs": Ref(Collection("xyz"), 1)
},
"block_2": {
"text": "Text2",
"refs": Ref(Collection("xyz"), 2)
}
}
FQL
Let({
blocks: Select(["data", "blocks"], Get(Ref(Collection("Blocks"), "299664869783765505"))),
entities: ToArray(Var("blocks")),
find: Filter(Var("entities"), entity => Equals(Select([1,"refs"], entity), Ref(Collection("xyz"), "1")))
},
ToObject(Var("find"))
)