【问题标题】:Need to compare array in Marklogic with xquery需要将 Marklogic 中的数组与 xquery 进行比较
【发布时间】:2022-08-11 23:07:22
【问题描述】:

我需要将 MarkLogic 中的数组与 Xquery 进行比较。

查询参数:

{
  "list": {
    "bookNo": 13,
    "BookArray":[20,21,22,23,24,25]
  }
}

样本数据:

{
"no":01'
"arrayList"[20,25]
}

{
"no":02'
"arrayList"[20,27]
}

{
"no":03'
"arrayList"[20,23,25]
}

输出:

"no":01
"no":03

我需要返回“no”,其中 arraylist 中的所有值都应该与 bookArray 匹配。

【问题讨论】:

  • 您的示例包含无效的 JSON。请更新,以便人们可以更轻松地制作原型并帮助您。

标签: xquery marklogic marklogic-10


【解决方案1】:

好的。您没有解释实际数据是否在系统中。所以我做了一个例子,好像一切都在记忆中。

我选择将样本保留在 MarkLogic JSON 表示形式中,其中包含一些奇怪的东西,例如数字节点和数组节点。如果您深入研究它,为了使其更具可读性,我使用fn:data() 来减少冗长。实际上,如果这是一个内存操作并且我不能使用 Javascript,那么我会将 JSON 结构转换为映射。

这是一个帮助您探索的示例。我清理了 JSON 以使其有效,并且我的样本将三个样本包装在一个数组中。

xquery version "1.0-ml";

let $param-as-json := xdmp:unquote('{
  "list": {
    "bookNo": 13,
    "BookArray":[20,21,22,23,24,25]
  }
}')

let $list-as-json := xdmp:unquote('[
  {
  "no":"01",
  "arrayList":[20,25]
  },
  {
  "no":"02",
  "arrayList":[20,27]
  },
  {
  "no":"03",
  "arrayList":[20,23,25]
  }
]')


let $my-list := fn:data($param-as-json//BookArray)
return for $item in $list-as-json/*
  let $local-list := fn:data($item//arrayList)
  let $intersection := fn:data($item//arrayList)[.=$my-list]  
  where fn:deep-equal($intersection, $local-list)
  return $item/no

结果:

01
03

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-29
    • 1970-01-01
    • 1970-01-01
    • 2015-10-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多