marklogic - 我们如何在单个 XQuery 代码中获取所有 MarkLogic 数据库文档计数?
问题描述
我们需要使用 XQuery 生成报告,其中需要详细信息,例如数据库大小和数据库文档计数(文档总数)。
我们有下面的 XQuery 代码,我们可以从中获取数据库名称和数据库大小,但我们还希望包括 AdminUI 中的数据库文档计数。
for $db-id in xdmp:databases()
let $db-name := xdmp:database-name($db-id)
let $db-size :=
fn:sum(
for $f-id in xdmp:database-forests($db-id)
let $f-status := xdmp:forest-status($f-id)
let $space := $f-status/forest:device-space
let $f-name := $f-status/forest:forest-name
let $f-size :=
fn:sum(
for $stand in $f-status/forest:stands/forest:stand
let $stand-size := $stand/forest:disk-size/fn:data(.)
return $space
)
return $f-size
)
order by $db-size descending
return $db-name || " = " || $db-size
就像是
return $db-name || " = " || $db-size || "=" || $db-count
使用下面,我们可以获取单个数据库中的文档计数(无论在 QC 下拉列表中选择什么),但我需要在单个脚本中为所有数据库运行以下命令。
xdmp:estimate(doc())
对此有任何帮助或建议吗?我在这里先向您的帮助表示感谢。
解决方案
您可以检索document-count
withxdmp:forest-counts()
并将它们相加,就像您对 size 所做的那样:
declare namespace forest = "http://marklogic.com/xdmp/status/forest";
for $db-id in xdmp:databases()
let $db-name := xdmp:database-name($db-id)
let $db-size :=
fn:sum(
for $f-id in xdmp:database-forests($db-id)
let $f-status := xdmp:forest-status($f-id)
let $space := $f-status/forest:device-space
let $f-name := $f-status/forest:forest-name
let $f-size :=
fn:sum(
for $stand in $f-status/forest:stands/forest:stand
let $stand-size := $stand/forest:disk-size/fn:data(.)
return $space
)
return $f-size
)
let $db-count :=
fn:sum(
for $forest-id in xdmp:database-forests($db-id)
let $forest-counts := xdmp:forest-counts($forest-id)
return $forest-counts/forest:document-count/fn:data(.)
)
order by $db-size descending
return $db-name || " = " || $db-size || ", " || $db-count
如果您确实想使用,xdmp:estimate(doc())
那么您可以在选项中使用xdmp:invoke-function()
并指定database
要执行的内容:
declare namespace forest = "http://marklogic.com/xdmp/status/forest";
for $db-id in xdmp:databases()
let $db-name := xdmp:database-name($db-id)
let $db-size :=
fn:sum(
for $f-id in xdmp:database-forests($db-id)
let $f-status := xdmp:forest-status($f-id)
let $space := $f-status/forest:device-space
let $f-name := $f-status/forest:forest-name
let $f-size :=
fn:sum(
for $stand in $f-status/forest:stands/forest:stand
let $stand-size := $stand/forest:disk-size/fn:data(.)
return $space
)
return $f-size
)
let $db-count :=
xdmp:invoke-function(
function(){ xdmp:estimate(doc())},
<options xmlns="xdmp:eval">
<database>{$db-id}</database>
</options>)
order by $db-size descending
return $db-name || " = " || $db-size || ", " || $db-count
推荐阅读
- sorting - 击败模式的 Quicksort 与 Timsort?
- react-native - 在本机反应中在createDrawerNavigator项目上设置onpress?
- python - 将值重新分配给特定列并使用 pandas 与其余列合并
- resources - 使用基于代理的建模模拟有限的资源
- vim - 如何调试键绑定/命令行为的变化(
/ ctrl-p 为 ctrlp.vim vim 插件)? - excel - 按唯一 ID 和自动删除求和
- javascript - 如何获取用户输入并将其永久保存在变量中并在 window.open() 中使用该变量
- vue.js - 当我不使用 vue 文件时,如何在 vue 中拥有一个或非索引?
- python - 在熊猫数据框中将纪元列值转换为时间
- python - 使用 scipy.integrate.quad 时,增加正函数的界限会降低积分!如何解决?