显示表格视图的所有列 MarkLogic Optic API

问题描述 投票:0回答:1

我正在尝试构建 MarkLogic 10 中的光学查询,它将显示某个模板视图的所有行,问题是某些行需要根据其他列中是否有值来与其他表进行联接,而这不是所有行都有。

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”字段的“Children”文档的行,该字段允许它们在我的查询中执行联接。我正在寻找的输出是返回“Children”表视图中的所有行。如果没有“hasParent”字段链接到其他表,请将从我的

op:select()
函数中的“Parent”表中提取的那些列留空。

有没有办法使用条件或类似的东西来返回我在此查询中需要的所有文档?

marklogic marklogic-optic-api
1个回答
3
投票

我的理解是,您想要 SQL 左外连接

的 NoSQL 等效项

MarkLogic 光学连接-左-外

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()
© www.soinside.com 2019 - 2024. All rights reserved.