MarkLogic查询型视图动态列构建:currentDateTime无法动态更新
Query-Based View动态生成currentDateTime的解决方案
问题原因
使用op:generate-view创建查询型视图时,currentDateTime的具体时间戳会被静态固化到视图定义中,而非保留fn:current-dateTime()的动态计算逻辑——因为op:generate-view的作用是将当前查询的执行结果(包括计算出的时间值)持久化为视图,不是保存计算表达式本身。
正确实现步骤
要让Query-Based View支持动态时间戳,不能直接从模板视图导出,需要手动编写视图的XML配置,保留动态表达式:
- 在Schemas数据库中手动创建查询型视图的XML:
<view xmlns="http://marklogic.com/xdmp/view"> <name>myQBV</name> <schema-name>DataHub</schema-name> <columns> <column> <name>currentDateTime</name> <scalar-type>dateTime</scalar-type> <val>fn:current-dateTime()</val> <nullable>true</nullable> <invalid-values>ignore</invalid-values> </column> </columns> <query> <![CDATA[ op:from-view("MyTable", "MyView") => op:select(()) ]]> </query> </view>
- 验证动态效果
使用你原有的Optic查询语句执行,每次返回的currentDateTime都会是查询执行时刻的实时时间戳:
let $QBV := op:from-view("DataHub", "myQBV") return $QBV =>op:select((op:view-col("myQBV", "currentDateTime"))) =>op:result()
核心提示
op:generate-view适用于固化静态结构的视图场景,但无法保留动态计算逻辑,因为它会将表达式的执行结果写入视图定义。- 手动定义视图时,
<query>块中的Optic计划会在每次查询视图时重新执行,<val>中的fn:current-dateTime()也会实时计算。
内容的提问来源于stack exchange,提问作者Tanner B
相关产品推荐
相关产品推荐

