Delete documents less than 30 days in Marklogic Xquery
我在互联网上搜索如何返回距离当前日期不到 1 个月的文档,我找到了上面的链接。由于我正在使用的元素没有范围索引
let $currentdt := fn:current-dateTime()
let $amonthAgo := $currentdt- xs:yearMonthDuration("P1M")
cts:search(doc(),
cts:element-query(xs:QName("myDateTimeField"), cts:true-query())
)[.//myDateTimeField[xs:dateTime(.) eq $amonthAgo]]
不返回任何内容。
我有示例值,例如
2021-05-18T06:32:34.761729-04:00
2021-05-15T06:32:34.761729-04:00
2021-03-19T06:32:34.761729-04:00
2021-06-15T06:32:34.761729-04:00
TIA!
最佳答案
除了 FLWOR 表达式缺少返回
之外,您还要求提供正好一个月前的所有匹配项,因为您使用的是 eq
在比较中。如果您想查找不到一个月的文档,您希望它们的日期晚正好是一个月前的时间点(即 $amonthAgo
)。您可以将谓词更正并简化为 xs:dateTime(myDateTimeField) gt $amonthAgo
。这是一个适合我的独立示例:
declare context item := document {
<doc>
<e id="1">
<myDateTimeField>2021-05-18T06:32:34.761729-04:00</myDateTimeField>
</e>
<e id="2">
<myDateTimeField>2021-05-15T06:32:34.761729-04:00</myDateTimeField>
</e>
<e id="3">
<myDateTimeField>2021-03-19T06:32:34.761729-04:00</myDateTimeField>
</e>
<e id="4">
<myDateTimeField>2021-06-15T06:32:34.761729-04:00</myDateTimeField>
</e>
</doc>
};
let $currentdt := fn:current-dateTime()
let $amonthAgo := $currentdt - xs:yearMonthDuration("P1M")
return //e[xs:dateTime(myDateTimeField) gt $amonthAgo]
结果(2021年6月19日):
<e id="4">
<myDateTimeField>2021-06-15T06:32:34.761729-04:00</myDateTimeField>
</e>
关于Marklogic 中的 Xquery 返回距离当前日期小于 1 个月的日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68046425/