在 Fauna 数据库中过滤文档的嵌套树?
Filtering nested tree of document in Fauna db?
我在 Fauna 中有以下单个文档的数据结构:
"data": {
"title": "Title1",
"blocks": [
{
"block_1": {
"text": "Text1",
"refs": Ref(Collection("xyz"), "XYZ")
},
{
"block_2": {
"text": "Text2",
"refs": Ref(Collection("xyz"), "XYZ2")
}
]
}
说给文件。
FQL 中是否有一种方法可以根据“refs”值获取“块”?例如。只有 return “block_1” 基于 “refs” = XYZ。
IE。上面的例子应该只 return “block_1”.
我希望问题很清楚。随时要求澄清。 :slight_smile:
感谢您的帮助。
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"))
)
但我建议修改 blocks
结构。尝试将其保存为数组
"blocks": [
{"text": "Text1", "refs": Ref(Collection("xyz"), 1) },
{"text": "Text2", "refs": Ref(Collection("xyz"), 2) }
]
所以 FQL 会是
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")
)
或对象
"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"))
)
我在 Fauna 中有以下单个文档的数据结构:
"data": {
"title": "Title1",
"blocks": [
{
"block_1": {
"text": "Text1",
"refs": Ref(Collection("xyz"), "XYZ")
},
{
"block_2": {
"text": "Text2",
"refs": Ref(Collection("xyz"), "XYZ2")
}
]
}
说给文件。 FQL 中是否有一种方法可以根据“refs”值获取“块”?例如。只有 return “block_1” 基于 “refs” = XYZ。 IE。上面的例子应该只 return “block_1”.
我希望问题很清楚。随时要求澄清。 :slight_smile:
感谢您的帮助。
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"))
)
但我建议修改 blocks
结构。尝试将其保存为数组
"blocks": [
{"text": "Text1", "refs": Ref(Collection("xyz"), 1) },
{"text": "Text2", "refs": Ref(Collection("xyz"), 2) }
]
所以 FQL 会是
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")
)
或对象
"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"))
)