You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用单条XQuery代码获取MarkLogic所有数据库的文档计数及容量

MarkLogic全库信息统计XQuery实现方案

你可以使用xdmp:invoke-function()指定执行上下文数据库,来逐个统计每个数据库的文档总数,修改后的完整代码如下:

xquery version "1.0-ml";
import module namespace forest = "http://marklogic.com/xdmp/status/forest" at "/MarkLogic/status-forest.xqy";

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)
    return fn:sum(
      for $stand in $f-status/forest:stands/forest:stand
      return $stand/forest:disk-size/fn:data(.)
    )
  )
(: 跨库获取当前数据库文档总数 :)
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 fn:concat($db-name, "  |  容量:", $db-size, " 字节  |  文档总数:", $db-count)

说明

  • 执行该脚本需要拥有所有数据库的读取权限,建议使用管理员角色账号运行
  • xdmp:estimate直接读取森林索引中的计数结果,性能极高,返回值和AdminUI展示的文档数完全一致,无全库扫描开销
  • 上述代码同时修正了原容量统计逻辑的偏差,统计结果和AdminUI的数据库容量展示对齐

内容的提问来源于stack exchange,提问作者SinghVK

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.15 05:15:04