Xquery 在MarkLogic中插入文档并在同一事务中读取

Xquery 在MarkLogic中插入文档并在同一事务中读取,xquery,marklogic,Xquery,Marklogic,下面是我用于其中一个功能的代码片段 declare function local:matchCounts($Id as xs:string, $status as xs:string) as xs:int { xdmp:estimate(cts:search(/count, cts:and-query(( cts:element-attribute-value-query(xs:QName("count"), xs:QName("Id"), $Id, "exact"), ct

下面是我用于其中一个功能的代码片段

declare function local:matchCounts($Id as xs:string, $status as xs:string) as xs:int {
  xdmp:estimate(cts:search(/count, cts:and-query((
    cts:element-attribute-value-query(xs:QName("count"), xs:QName("Id"), $Id, "exact"),
    cts:element-attribute-value-query(xs:QName("child"), xs:QName("MatchStatus"), $status, "exact")
  )), "unfiltered"))
};

declare function local:saveCountsMatchC($Id as xs:string) {
  let $evenCount := local:matchCounts($Id, "even")
  let $oddCount := local:matchCounts($Id, "odd")
  return ($evenCount, $oddCount)
};

declare function local:matchingProcess($Id as xs:string) {
let $total-records := 1000
let $batch-size := 50
let $pagination := 0
let $bs := 
   for $records in 1 to fn:ceiling($total-records  div $batch-size )
   let $start := fn:sum($pagination + 1)
   let $end := fn:sum($batch-size + $pagination)
   let $_ := xdmp:set($pagination, $end)
   return
    xdmp:spawn-function
    (
    function() {
     for $each at $pos in ($start to $end)
     let $id := sem:uuid-string()
     let $xml := if(($pos mod 2) eq 0) then <count Id='{$Id}'><child MatchStatus='even'></child></count> 
                 else <count Id='{$Id}'><child MatchStatus='odd'></child></count>
     return xdmp:document-insert(concat("/", $id, ".xml"), $xml)
    },
    <options xmlns="xdmp:eval"><result>{fn:true()}</result><commit>auto</commit><update>true</update></options>
    )
let $_ := $bs
return local:saveCountsMatchC($Id)
};

local:matchingProcess("1")
将函数local:matchCounts($Id为xs:string,$status为xs:string)声明为xs:int{
xdmp:估计(cts:搜索(/count,cts:和查询((
cts:element属性值查询(xs:QName(“count”)、xs:QName(“Id”)、$Id、“exact”),
cts:element属性值查询(xs:QName(“子”),xs:QName(“MatchStatus”),$status,“精确”)
)),“未过滤”))
};
声明函数local:saveCountsMatchC($Id为xs:string){
let$evenCount:=local:matchCounts($Id,“偶数”)
让$oddCount:=本地:匹配计数($Id,“奇”)
返回($evenCount,$oddCount)
};
声明函数local:matchingProcess($Id为xs:string){
让$total记录:=1000
让$batch size:=50
让$pagination:=0
let$bs:=
对于1至fn中的$records:上限($total records div$batch size)
让$start:=fn:sum($pagination+1)
让$end:=fn:sum($batch size+$pagination)
let$979;:=xdmp:set($pagination,$end)
回来
xdmp:spawn函数
(
函数(){
在$pos in($start-to$end)中,每个$pos的$
让$id:=sem:uuid-string()
设$xml:=if($pos mod 2)eq 0)then
其他的
返回xdmp:文档插入(concat(“/”,$id,.xml”),$xml)
},
{fn:true()}autotrue
)
设$\:=$bs
返回本地:saveCountsMatchC($Id)
};
本地:匹配过程(“1”)
这里的要求是使用50的批大小迭代1000个文档,所以基本上我使用spawn函数创建20个50大小的批,在我的数据库中插入1000个文档。 插入这些文档后,我需要在同一事务中读取这些文档。这里有500个文档具有MatchStatus='奇数',500个文档具有MatchStatus='偶数' 查询应返回(500500)作为输出;相反,它返回(0,0)

我正在使用
{fn:true()}
选项,以便我的下一条语句等待所有spawn任务完成,但它没有发生

有人能帮我满足这个要求吗


注意:需要插入1000个文档,然后仅在同一个函数调用中读取它们

执行生成的代码本身不执行更新,因此将以所谓的查询模式运行。在查询模式下,只有代码开始之前的更新才可见

您可以尝试在更新模式()下运行,但通常更容易生成或评估更新的计数/读取。例如,将
xdmp:estimate
包装在
xdmp:spawn函数
中,同时使用结果
true


像这样的问题总是让我想知道为什么要这样做。退一步,问问自己是否真的需要在同一个电话中完成所有这些,这可能是明智的。在一次呼叫中执行多个更新无法扩展,同时尝试读取结果也不会使其变得更简单。。