在我试图构建的MarkLogic 10中有一个Optic查询,它将显示某个模板视图的所有行,问题是如果它在其他列中有值,而不是所有行都有值,则一些行需要连接到其他表。
TEMPLATE VIEW: CHILDREN
Example document:
{
id: "11"
childName: "Mark"
}
Example document2:
{
id: "22"
childName: "Jacob"
hasParent: "33"
}
TEMPLATE VIEW: PARENT
Example document3:
{
id: "33"
name: "Tom"
role: "Dad"
}我需要返回响应中的文档1和2,从父视图中链接到它的字段,从父文档返回列(如果文档中有hasParent键)。
在当前的光学查询中,如果有一个op:inner-join()将示例文档链接到其他表以提取所需的所有列,则响应中不会返回第一个示例文档,因为它没有hasParent字段。
查询:
let $children := op:from-view("Test", "Children")
let $parent := op:from-view("Test", "Parent")
return $children
=>op:join-inner($parent, op:on(
op:view-col("Children", "hasParent"),
op:view-col("Parent", "id")))
=>op:where(op:eq(op:view-col('Children', 'hasParent'), op:view-col('Parent', 'id'))
=>op:select((op:view-col("Children", "id"),
op:view-col("Children", "name"),
op:view-col("Parent", "id"),
op:view-col("Parent", "name"),
op:view-col("Parent", "role")
))
=>op:result()上面的查询只返回具有"hasParent“字段的”子“文档的行,该字段允许它们在我的查询中执行联接。我要寻找的输出是返回“子”表视图中的所有行。如果没有"hasParent“字段链接到其他表,则将从op:select()函数中的”父“表中提取的列保留为空白。
有没有一种方法可以使用条件或类似的东西来返回我在这个查询中需要的所有文档?
发布于 2022-07-25 23:25:36
我的理解是,您希望使用与SQL NoSQL等价的SQL左侧联接。
let $children := op:from-view("Test", "Children")
let $parent := op:from-view("Test", "Parent")
return $children
=> op:join-left-outer($parent, op:on(
op:view-col("Children", "hasParent"),
op:view-col("Parent", "id")))
=> op:select((
op:view-col("Children", "id"),
op:view-col("Children", "name"),
op:view-col("Parent", "id"),
op:view-col("Parent", "name"),
op:view-col("Parent", "role")
))
=> op:result()https://stackoverflow.com/questions/73113030
复制相似问题