Balan
Balan

Reputation: 393

Need to compare array in Marklogic with xquery

I need to compare array in MarkLogic with Xquery .

Query parameters:

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

Sample Data:

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

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

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

Output:

"no":01
"no":03

I need to return "no" where all values from arraylist should be match with bookArray.

Upvotes: 0

Views: 143

Answers (1)

Ok. You do not explain if the actual data is in the system or not. So I did an example as if it is all in memory.

I chose to keep the sample in the MarkLogic JSON representation which has some oddities like number-nodes and array-nodes under the hood. To make it more readable if you dig into it, i used fn:data() to get less verbose. In all reality, if this was an in-memory operation and I could not use Javascript, then I would have converted the JSON structures to maps.

Here is a sample to help you explore. I cleaned up the JSON to be valid and for my sample wrapped the three samples in a single array.

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

Result:

01
03

Upvotes: 2

Related Questions